Feed Sign in with OpenID OpenID

Simon Willison’s Weblog

Evil but sometimes unavoidable

I’ve seen a few questions on various forums and mailing lists asking if there is a way of using target=“blank” on links in XHTML Strict without running in to a validation error. I’ve put together an example page showing a method of fixing this using the DOM, which also demonstrates Andrew Clover’s fun-but-ugly Evil Mangled Comments Hack.

This is Evil but sometimes unavoidable by Simon Willison, posted on 23rd July 2002.

View blog reactions

Next: Excite UK now powered by AllTheWeb

Previous: Swannie's blog

6 comments

  1. Hm. This is on shaky moral ground, this. It validates because the validator doesn't run JavaScript; all you're doing is creating invalid markup at runtime rather than at design time, which is still a naughty thing to do. Is there no XHTML-valid way of opening links in new windows at all? Are the W3C insistent that you use JavaScript for this?

    sil - 23rd July 2002 13:15 - #

  2. I thought about that, but I don't think it's dodgy at all. The document is valid XHTML, which means anything that wants to read it with an XML parser and the DTD can do so. The target="_blank" script will only work in user agents that support both javascript and target="_blank" - anything else will rightly ignore it. As such it's more of a javascript document "enhancement" than a cheap hack to trick the validator. I haven't seen an XHTML valid way of opening new windows, which I'm guessing is because XHTML is about describing a document, not how the user agent should behave when you interact with that document.

    Simon - 23rd July 2002 13:42 - #

  3. I'm only passingly familiar with Javascript, so I may be completely wrong. Couldn't you link this from a .js file at the end of the document? Or call it from onLoad? That would be XHTML compliant.

    Micah - 24th July 2002 01:00 - #

  4. People don't need _blank windows shoved down their throats, what they need is education in how to control their UA, so they can choose their own style of browsing and open new windows as THEY like (instead of how YOU like it as an author). There are various reasons in defense of not using blank windows, like continuity of browsing, the sheer annoyance of yet another window that forces itself onto the user, various methods that people already use to block new windows, but most of all, disrespect to a visitors choice. My back button is there for a reason and I know how to use it. I am not sure if the W3C had this in mind when abandoning the target attribute, but if it takes a step like this to change a mindset, then they have my vote.

    Kris - 24th July 2002 08:32 - #

  5. Kris I agree completely - in fact I link to Mark Pilgrim's accessibility tips on opening new windows (i.e don't do it) from the example page. I put the example together because I'm keen on helping people migrate to XHTML and one of the most common complaints about the change is the lack of the target="" attribute. If having a hack for target="_blank" helps convince people to write XHTML we can concentrate on weaning them off new windows later on ;)

    Simon - 24th July 2002 10:20 - #

  6. People who want to control their UA can do things like write their own personal style sheets, and download browsers like Mozilla which allow you to selectively turn off new windows, or turn off Javascript altogether. Getting target="_blank" out of the HTML spec was the right idea but new windows do have their uses, and should be opened through JS/ECMAscript alone.

    Micah - 25th July 2002 03:11 - #

Comments are closed.

Previously hosted at http://simon.incutio.com/archive/2002/07/23/evilButSomeTimesUnavoidable

A django site