Nominated on

csswinner.com cssdesignawards.com
Sunday, November 20, 2011 - by

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

Usage:

Method 1:
Without passing any extensions string

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

Examples:

 Javascript |  copy code |? 
1
//Option 1
2
$("a").extLink();
3
//Option 2
4
$("#content a").extLink();
5
//Option 3
6
$(".leftpane a,.partners a").extLink();
7

 

Method 2:
When passed with extensions string

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

Examples:

 Javascript |  copy code |? 
1
$("a").extLink({fileTypes:".doc,.pdf"});

 

Download Source

extLink

No Comments

Post Your Comments

Name (required)
Email Address (will not be published) (required)