XHTML valid external links with mootools

W3C no longer include the target attribute of the <a> tag in HTML 4.0 Strict and XHTML 1.0 Strict document types . To make our links open new windows we will have to use Javascript, and with mootools it make this task very easy. Just add this snippet to the bottom of your html document and it will look for all external links on your page and add the target attribute to each of the links.

$$('a[href^=http]').each(function(a) {
    if (window.location.hostname) {
        var hostname = window.location.hostname.replace("www.", "").toLowerCase();
        if (!a.get('href').contains(hostname)) {
            a.set({
                'target': '_blank',
                'class': (a.get('class')) ? a.get('class') + ' external' : 'external'
            });
        }
    }
});

Leave a comment

Bookmark with

Post a comment

About

Welcome to my online portfolio and development site where i can share my developments and projects.

Ive been developing for 7 years working mainly with online applications and websites.

Download my CV or

Favorites

RSS Feed Subscribe
RSS or Atom

Copyright 2010 kd3sign.co.uk     Best viewed in Mozilla Firefox
hosted by Media Temple powered by CodeIgniter