Netcars provide cheap car loans and instant car finance online.
Great deals on car insurance from Direct Line
Save money - get a car insurance quote from privilege
adverts
 

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!

Tagged , , ,
 
 

Don't forget Dazecoop

 
keep track of my tweets :)
 
For cheaper car insurance, there is only one place to go and that's Hastings Direct!

9 comments so far

  • Collin
    21 Oct 2008 at 5:10 pm

    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!

  • Daze
    21 Oct 2008 at 5:13 pm

    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 :)

  • Brooke Bryan
    22 Oct 2008 at 12:08 am

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

  • Daze
    22 Oct 2008 at 8:23 am

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

  • Jasper
    22 Oct 2008 at 5:51 pm

    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;
    });
    }

  • Daze
    22 Oct 2008 at 6:49 pm

    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 :)

  • Jasper
    22 Oct 2008 at 7:39 pm

    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.

  • Daze
    23 Oct 2008 at 1:17 am

    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!

  • Bogdan Cluj
    24 Jun 2009 at 10:59 am

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

What do you think?

 
wordpress
Theme by David Cooper :)