Skip to content

Instantly share code, notes, and snippets.

@slav123
Created April 2, 2012 02:16
Show Gist options
  • Save slav123/2280030 to your computer and use it in GitHub Desktop.
Save slav123/2280030 to your computer and use it in GitHub Desktop.
make active url's from javascript
jQuery('#last_tweet').urlize();
jQuery.fn.urlize = function() {
if (this.length > 0) {
this.each(function(i, obj){
// making links active
var x = jQuery(obj).html();
var list = x.match( /\b(http:\/\/|www\.|http:\/\/www\.|https:\/\/www\.|https:\/\/)[^ <]{2,200}\b/g );
if (list) {
for ( i = 0; i < list.length; i++ ) {
var prot = list[i].indexOf('http://') === 0 || list[i].indexOf('https://') === 0 ? '' : 'http://';
x = x.replace( list[i], "<a target='_blank' href='" + prot + list[i] + "'>"+ list[i] + "</a>" );
}
}
jQuery(obj).html(x);
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment