自动识别URL

This commit is contained in:
yuanke 2016-05-19 16:24:57 +08:00
parent c208b79e2d
commit 7da1418069
2 changed files with 58 additions and 1 deletions

View File

@ -1,6 +1,6 @@
<div class="homepagePostIntro break_word upload_img maxh360 table_maxWidth " id="activity_description_<%= user_activity_id%>">
<div id="intro_content_<%= user_activity_id%>">
<%= content.to_s.html_safe%>
<%= content.to_s.html_safe %>
</div>
</div>
<script type="text/javascript">
@ -13,6 +13,9 @@
postContent=postContent.replace(/&nbsp; /g,"&nbsp;&nbsp; ");
$(this).html(postContent);
});
autoUrl('intro_content_<%= user_activity_id%>');
description_show_hide(<%=user_activity_id %>);
});
</script>

View File

@ -1291,3 +1291,57 @@ function pop_up_box(value,tWidth,tTop,tLeft){
}
//yk 自动识别URL 并加上链接
var strRegex = '((https|http|ftp|rtsp|mms)?://)'
+ '?(([0-9a-z_!~*\'().&=+$%-]+: )?[0-9a-z_!~*\'().&=+$%-]+@)?' //ftp的user@
+ '(([0-9]{1,3}.){3}[0-9]{1,3}' // IP形式的URL- 199.194.52.184
+ '|' // 允许IP和DOMAIN域名
+ '([0-9a-z_!~*\'()-]+.)*' // 域名- www.
+ '([0-9a-z][0-9a-z-]{0,61})?[0-9a-z].' // 二级域名
+ '[a-z]{2,6})' // first level domain- .com or .museum
+ '(:[0-9]{1,4})?' // 端口- :80
+ '((/?)|' // a slash isn't required if there is no file name
+ '(/[0-9a-z_!~*\'().;?:@&=+$,%#-]+)+/?)';
function autoUrl(id){
if ($("#"+id).children().length > 0 ){
$("#"+id+" p,#"+ id +" span,#"+id+" em,#"+id+" h1,#"+id+" h2,#"+id+" h3,#"+id+" h4,#"+id+" strong,#"+id+" b,#"+id+" font,#"+id+" i").each(function(){
if ($(this).children().length == 0){
var html = $(this).text(); //.replace(/(^\s*)|(\s*$)/g, "")
console.log("html="+html);
var re=new RegExp(strRegex,"g");
html = html.replace(re,function(full) {
//没有://的都加上http://
if (full.indexOf("://") >= 0){
var reStr = "<a target='_Blank' href="+"'"+full+"'"+">"+full+"</a>";
}
else{
var reStr = "<a target='_Blank' href="+"'http://"+full+"'"+">"+full+"</a>";
}
console.log("reStr="+reStr);
return reStr;
});
$(this).html(html);
}
});
}
else{
var html = $("#"+id).text(); //.replace(/(^\s*)|(\s*$)/g, "")
console.log("html="+html);
var re=new RegExp(strRegex,"g");
html = html.replace(re,function(full) {
//没有://的都加上http://
if (full.indexOf("://") >= 0){
var reStr = "<a target='_Blank' href="+"'"+full+"'"+">"+full+"</a>";
}
else{
var reStr = "<a target='_Blank' href="+"'http://"+full+"'"+">"+full+"</a>";
}
console.log("reStr="+reStr);
return reStr;
});
$("#"+id).html(html);
}
}