Open new window, target blank replacement for XHTML Strict code

Making a site XHTML Strict and valid can be a little bit of a challenge at first – especially if like me you’re used to very relaxed and quite frankly, non-valid code!

One of the specific things that I found a little annoying when trying to make this site valid is the fact that target=”_blank” is not valid in the XHTML Strict standards. Eh? How’d you get around that then…

Javascript, or better, jQuery of course :)

Here’s a bit of jQuery code that I found elsewhere on the ‘net that I thought I should share.

$('a.new-window').click(function(){
   window.open(this.href);
   return false;
});

Simply dump that into your jQuery document ready function in your JS file.

Then, if you want to open a link in a new window now, instead of using the target=”_blank” method, simply use class=”new-window” on the <a> tag. Sweeeet!

 

9 comments about Open new window, target blank replacement for XHTML Strict code

  1. Collin says:

    That’s all fine and dandy but in removing the requirement for the depracated “_blank” target you’re still breaking widely acknowledged “netiquette” by forcing the link to open in a new window or tab.

    I much prefer to allow the user to control where they want the new link to open – as does most of the rest of the web!

  2. Daze says:

    I sort of agree with you, but in some cases its just a requirement (I think) for some links to open in a new window or tab.

    Also (just tested), but the above code, as default, opens in a new tab not a completely new windows – at least on Firefox 3.x on OSX…

    You’ll notice some of the links on this site have the above code applied to them :)

  3. Brooke Bryan says:

    So, no links back to my site after I showed you this little cool feature ;)

  4. Daze says:

    Well…. I sort’a changed the way you showed me a little bit, but um…
    visit http://www.bajb.net/ for cool stuff :P

  5. Jasper says:

    I agree with Colin – I’ll open in a new window if I want – but when it’s invariably thrust upon me that links should open in a new window, I used this for external links (mootools warning :p)

    var externallinks = function(){
    document.getElements(‘a’).addEvent(‘click’,function(){
    if (this.get(‘href’).toString().substr(0,7) == ‘http://’ && (this.get(‘href’).toString().match(/^http:\/\/[(www.)]*do1\.uk\.com/) == null)){
    window.open(this.get(‘href’));
    return false;
    }
    return true;
    });
    }

  6. Daze says:

    Ah yeh fair play. Looking at that code, I understand it and it doesn’t look too far off of jQuery – I guess afterall it is based on the same structure…

    I think though, agreeing with Jasper here, that external links should indeed open in a new window in almost all cases – but that’s just me :)

  7. Jasper says:

    If you care about your stats then it’s a good idea to as it drastically lowers your bounce rate, but I prefer to let people decide whether they want to stay or not.

    mootools looks the same as normal javascript imo – especially that as there’s only really 3 functions in there that are part of mootools and they hook into existing javascript objects :D there are $ functions, but I don’t tend to use them a lot as they’re there as part of the core and are folded into higher level libraries.

  8. Daze says:

    Cool beans, thanks for your thoughts on this. Very interesting. I liked your code quite a bit… so I’ve gone & written another post about it, jQuery-equivillent.

    … Also just found that – I think – my blog is not yet showing trackbacks / pingbacks. My bad!

  9. Bogdan Cluj says:

    Very great stuff. finally can validate xhtml strict and put my links in blank. Thanks a lot !

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>