/**
* author Redcrow
* www.na5cent.blogspot.com
* 29/01/2013
*/
Urls = {
urlRegExPattern : /(\b(((https?|ftp|file):\/\/)|(www\.))+[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,
detectLink : function(text){
var replace = text.replace(/&/g, '&')
.replace(/>/g, '>')
.replace(/</g, '<')
.replace(/"/g, '"')
.replace(this.urlRegExPattern, function(currentUrl){
var urlLink = currentUrl;
var urlText = currentUrl;
if(!urlLink.match(/\b(https?)/)){
urlLink = "http://" + urlLink;
}
return "<a href='" + urlLink + "' target='_blank'>" + urlText + "</a>";
});
return replace;
}
}
/*
* example to use
* window.onload = function(){
* $("#sendButton").click(function(){
* var value = $("#inputText").val();
* var result = Urls.detectLink(value);
* $("#result").html(result);
* });
* };
*/
วันอังคารที่ 29 มกราคม พ.ศ. 2556
detect html link and protect XSS javascript
สมัครสมาชิก:
ส่งความคิดเห็น (Atom)

ไม่มีความคิดเห็น:
แสดงความคิดเห็น