extLink a jQuery plugin to add target=”_blank” to external links
I had been working with jQuery quite a lot and develop quite some interactive widgets and AJAX rich modules never got a chance to actually develop my code into a jQuery plugin. Until last week had been reading various articles and books on jQuery plugin development and the patterns associated with those.
In order to authenticate my knowledge I had written a small jQuery plugin which has been written by quite some jQuery plugin authors in the past. What the plugin does is checks for current host and if the anchor’s href contains link outside of the current page hosting domain will add an attribute of target=”_blank”.
Without passing any extensions string
It will find all the anchors based on the selector string provided that does not have the same domain as of the current page and will attach target=”_blank” attribute to them.
$(".leftpane a,.partners a").extLink();
When passed with extensions string
Rest of the functionality will be similar as the Method 1 but it will attach target=”_blank” to the links ending with the files extension provided by the fileTypes options parameter.