9 replies [Last post]
Katie
Katie's picture
Offline
Enthusiast
Seattle, WA
Last seen: 3 years 9 weeks ago
Seattle, WA
Timezone: GMT-7
Joined: 2006-08-06
Posts: 357
Points: 2

I've got a site using the XHTML 1.0 Strict doctype, and we've got a few pages where we want to open the links in a new window.

The code I was delivered uses target="_blank" to accomplish this, but the W3C validator says that target isn't a valid attribute.

What method would you recommend that I use to open some of the links in a new window?

Thanks!

Blog: Pew Pew Laser Blog
Online File Storage: DropBox
Daily Deals on Local Activities: Groupon

Tags:
Chris..S
Chris..S's picture
Offline
Moderator
Last seen: 1 year 44 weeks ago
Timezone: GMT+1
Joined: 2005-02-22
Posts: 6078
Points: 173

The "strictly" correct

The "strictly" correct method is to setup an onclick handler for the links and do an "OpenWindow()" on click. The "it works" method (and the method I use) is to give a particular attribute value (class or rel) to those links which you want to open in a new window and have javascript parse the links after loading and add target="_blank" to those elements.

// jquery document.ready = function() { $('a.new').attr('target','_blank'); // or use 'a[rel=external]' }

Katie
Katie's picture
Offline
Enthusiast
Seattle, WA
Last seen: 3 years 9 weeks ago
Seattle, WA
Timezone: GMT-7
Joined: 2006-08-06
Posts: 357
Points: 2

Chris, Thanks for the

Chris,

Thanks for the solution. I feared it was going to be something that troublesome.

I may just be better off putting some logic in the header to use XHTML Transitional for those pages.

Blog: Pew Pew Laser Blog
Online File Storage: DropBox
Daily Deals on Local Activities: Groupon

Tyssen
Tyssen's picture
Offline
Moderator
Brisbane
Last seen: 49 weeks 6 hours ago
Brisbane
Timezone: GMT+10
Joined: 2004-05-01
Posts: 8201
Points: 1386

Katie wrote:I may just be

Katie wrote:
I may just be better off putting some logic in the header to use XHTML Transitional for those pages.

Why bother? If you know that the rest of your page is valid apart from that one aspect and that that particular validation error isn't going to cause any rendering problems, what worry about it?

How to get help
Post a link. If you can't post a link, jsFiddle it.
My blog | My older articles | CSS Reference

gary.turner
gary.turner's picture
Offline
Moderator
Dallas
Last seen: 6 hours 48 min ago
Dallas
Timezone: GMT-5
Joined: 2004-06-25
Posts: 9149
Points: 3133

If you're not using a

If you're not using a library like jquery, you can use your own function.

window.onload = function() {
el = document.getElementsByTagName("a");
for (var i=0; iDepends on the links including rel="ext".

cheers,

gary

Unplanned code results in a tangled wad of brain-cramping confusion.

There are enough html & css demos and tutorials to be interesting. Please visit.

Chris..S
Chris..S's picture
Offline
Moderator
Last seen: 1 year 44 weeks ago
Timezone: GMT+1
Joined: 2005-02-22
Posts: 6078
Points: 173

There is no need for that

There is no need for that first line (getElementsByTagName), document already has an array of "A" elements, innovatively called, document.anchors.

gary.turner
gary.turner's picture
Offline
Moderator
Dallas
Last seen: 6 hours 48 min ago
Dallas
Timezone: GMT-5
Joined: 2004-06-25
Posts: 9149
Points: 3133

It did slip my mind, there's

It did slip my mind, there's that and forms and, it seems (my mind's gone target="_blank"), something else, too. I think I'd still have the first line as:

el = document.anchors;

It'd save all that ugly camel notation.

cheers,

gary

Unplanned code results in a tangled wad of brain-cramping confusion.

There are enough html & css demos and tutorials to be interesting. Please visit.

Katie
Katie's picture
Offline
Enthusiast
Seattle, WA
Last seen: 3 years 9 weeks ago
Seattle, WA
Timezone: GMT-7
Joined: 2006-08-06
Posts: 357
Points: 2

Tyssen wrote:Why bother? If

Tyssen wrote:
Why bother? If you know that the rest of your page is valid apart from that one aspect and that that particular validation error isn't going to cause any rendering problems, what worry about it?

Well, I guess there are a couple of reasons why I'm bothered about this. One is that my company hired an agency, and the code that they delivered is crappy. So, as the put-upon web master, I'm on this big kick to fix everything. Another is that I give people such a hard time about validation, that I feel really compelled to get all of my stuff absolutely perfect.

I guess beyond my feelings that everything must validate, I don't need it to validate now. I do appreciate everyone's feedback, and I'm quite intrigued by the JavaScript solutions. Thank you. Smile

Blog: Pew Pew Laser Blog
Online File Storage: DropBox
Daily Deals on Local Activities: Groupon

Ed Seedhouse
Ed Seedhouse's picture
Offline
Guru
Victoria British Columbia
Last seen: 7 weeks 28 min ago
Victoria British Columbia
Timezone: GMT-7
Joined: 2005-12-14
Posts: 3570
Points: 675

Katie wrote:I guess beyond

Katie wrote:
I guess beyond my feelings that everything must validate, I don't need it to validate now. I do appreciate everyone's feedback, and I'm quite intrigued by the JavaScript solutions. Thank you. Smile

Well personally if I want a page to do something that doesn't validate under a strict doctype the first thing I want to do is to think really seriously about whether I must really do that.

Then if I decide I must indeed I would try to find some way to do it that is valid under my chosen doctype and if that wasn't possible, to find an alternate doctype under which it is legal.

However I do think it's a good point that anchors pointing to a target of _blank will indeed open up a new window under any modern browser no matter what doctype you are using it might be unnecessarily wasteful of time and effort to recode the entire page to validate under a new doctype. Still I'm the obsessive type and I don't think I could make myself follow that path no matter how reasonable it is...

Ed Seedhouse

Posting Guidelines

Watch out! I am carrying irony, sarcasm and satire, and know how to use them.

Chris..S
Chris..S's picture
Offline
Moderator
Last seen: 1 year 44 weeks ago
Timezone: GMT+1
Joined: 2005-02-22
Posts: 6078
Points: 173

Ed Seedhouse wrote:However I

Ed Seedhouse wrote:
However I do think it's a good point that anchors pointing to a target of _blank will indeed open up a new window under any modern browser no matter what doctype you are using it might be unnecessarily wasteful of time and effort to recode the entire page to validate under a new doctype. Still I'm the obsessive type and I don't think I could make myself follow that path no matter how reasonable it is...

Why would you need to recode an entire page? Its simply a change from strict to transitional.