XHTML valid external links with mootools


/ Published in: JavaScript
Save to your folder(s)

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 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.


Copy this code and paste it in your HTML
  1. $$('a[href^=http]').each(function(a) {
  2. if (window.location.hostname) {
  3. var hostname = window.location.hostname.replace("www.", "").toLowerCase();
  4. if (!a.get('href').contains(hostname)) {
  5. a.set({
  6. 'target': '_blank'
  7. });
  8. }
  9. }
  10. });

URL: http://www.kd3sign.co.uk/blog/post/xhtml-valid-external-links-with-mootools.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.