Mootools set file types to open in new window


/ Published in: JavaScript

From: David Walsh.
http://davidwalsh.name/dw-content/open-links.php


Copy this code and paste it in your HTML
  1. /* classes */
  2. var OpenLinks = new Class({
  3. //initialization
  4. initialize: function(file_extensions,override_targets,target,no_class) {
  5.  
  6. //analyze all anchors
  7. $$('a').each(function(el) {
  8.  
  9. //check each href for case-insensitive file extensions
  10. var str = el.get('href');
  11. var ext = str.substring(str.lastIndexOf('.') + 1,str.length)
  12. if(file_extensions.contains(ext.toLowerCase()) && ((override_targets || !el.get('target')) && !el.hasClass(no_class + '')))
  13. {
  14. el.setProperty('target',target ? target : '_blank');
  15. el.setStyle('color','#f00');
  16. }
  17. });
  18. }
  19. });
  20.  
  21. /* do it! */
  22. window.addEvent('domready', function() {
  23. //enter in as lower case
  24. var olinks = new OpenLinks(['doc','pdf','xls','jpg','gif','png'],1,'_blank','no-target');/works
  25. //var olinks = new OpenLinks(['doc','pdf','xls','jpg','gif','png'],1,'_blank');//works
  26. //var olinks = new OpenLinks(['doc','pdf','xls','jpg','gif','png'],1);//works
  27. //var olinks = new OpenLinks(['doc','pdf','xls','jpg','gif','png'],0);//works
  28. //var olinks = new OpenLinks(['doc','pdf','xls','jpg','gif','png']);//works
  29. });

URL: http://davidwalsh.name/dw-content/open-links.php

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.