Return to Snippet

Revision: 5955
at April 16, 2008 06:30 by gbot


Initial Code
/* classes */
var OpenLinks = new Class({
    //initialization
    initialize: function(file_extensions,override_targets,target,no_class) {
        
        //analyze all anchors
        $$('a').each(function(el) {
            
            //check each href for case-insensitive file extensions
            var str = el.get('href');
            var ext = str.substring(str.lastIndexOf('.') + 1,str.length)
            if(file_extensions.contains(ext.toLowerCase()) && ((override_targets || !el.get('target')) && !el.hasClass(no_class + '')))
            {
                el.setProperty('target',target ? target : '_blank');
                el.setStyle('color','#f00');
            }
        });
    }
});
		
/* do it! */
window.addEvent('domready', function() {
//enter in as lower case
var olinks = new OpenLinks(['doc','pdf','xls','jpg','gif','png'],1,'_blank','no-target');/works
//var olinks = new OpenLinks(['doc','pdf','xls','jpg','gif','png'],1,'_blank');//works
//var olinks = new OpenLinks(['doc','pdf','xls','jpg','gif','png'],1);//works
//var olinks = new OpenLinks(['doc','pdf','xls','jpg','gif','png'],0);//works
//var olinks = new OpenLinks(['doc','pdf','xls','jpg','gif','png']);//works
});

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

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

Initial Title
Mootools set file types to open in new window

Initial Tags
window

Initial Language
JavaScript