automatically send external links to new window with mootools


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

The html attribute 'target="_blank"' for links is no longer valid code as of XHTML Strict 1.0. This code will run using mootools on domready to send all clicks to new windows when going to an external domain.


Copy this code and paste it in your HTML
  1. window.addEvent('domready', function() {
  2. // modified from http://joesong.com/2009/11/external-links-in-new-window-passive-and-with-mootools/
  3. // to avoid using the 'target' attribute, which is not part of xhtml 1.0 strict
  4. var currentDomain = window.location.host;
  5. $(document.body).addEvent('click', function(evt) {
  6. var target = $(evt.target);
  7. if (target.get('tag') !== 'a') {
  8. target = target.getParent();
  9. }
  10.  
  11. if (target && target.get('tag') === 'a' && target.get('href').test('http') && !target.get('href').test(currentDomain)) {
  12. window.open(target.get('href'), '_blank');
  13. return false;
  14. }
  15. });
  16. });

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.