Created
March 7, 2014 07:59
-
-
Save rajib/9407297 to your computer and use it in GitHub Desktop.
Linkify Message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Utility = function () { | |
return { | |
linkify: function (text) { | |
if (text) { | |
text = text.replace( | |
/((https?\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi, | |
function(url){ | |
var full_url = url; | |
if (!full_url.match('^https?:\/\/')) { | |
full_url = 'http://' + full_url; | |
} | |
return '<a href="' + full_url + '" target="_blank">' + url + '</a>'; | |
} | |
); | |
} | |
return text; | |
}, | |
// applicable for a single dom element ID, paramater should be like '#elementId' | |
linkifyMessage: function (dom_id) { | |
$(dom_id).html( | |
Utility.linkify($(dom_id).text()) | |
) | |
}, | |
// applicable for a list of element selected by jquery CSS selector, paramater should be like '.css-class' | |
linkifyMessages: function (dom_css_selector) { | |
var message_doms = $(dom_css_selector); | |
$.each( message_doms, function() { | |
$(this).html( | |
Utility.linkify($(this).text()) | |
) | |
}); | |
} | |
} | |
}(); | |
// Example | |
Utility.linkifyMessage('#elementId'); | |
Utility.linkifyMessages('.css-class'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment