Automatically add URL/email link by shawl. Qiu to JavaScript strings

Source: Internet
Author: User
Automatically add URL/email link by shawl. Qiu to JavaScript strings

Note:
In fact, I have written at least 10 codes for automatically appending URL/Email links.
Until recently, I learned about JS xiaocheng and made a perfect Dom solution, but I can add links without errors.
But the problem always occurs. Although the DOM method can be used to add links without errors, the indent format is lost ....
This is fatal, so it overwrites a record that won't lose indentation.
This is a direct string processing, and errors may occur, but the error rate will be quite low.
 
Directory:
1. Dom appending URL/email link
2. append URL/email link to the string
 
Shawl. Qiu
2006-12-13
Http://blog.csdn.net/btbtd
 
1. Dom appending URL/email link

 
 
  1. <script type="text/javascript">
  2. //<![CDATA[
  3. onload=function(){
  4. var o_=document;
  5. fDomAtcLink(o_.body, o_);
  6. }
  7. //sqMethod.attachlink(o_.body, o_)
  8. function fDomAtcLink(obj, dcu){
  9. var temp, str='', span;
  10. for(var i=0, j=obj.childNodes.length; i<j; i++){
  11. temp=obj.childNodes[i];
  12. if(temp.nodeType==3){
  13. if(temp.parentNode.nodeName=='A')return false;
  14. if(temp.data.indexOf('http')<0&&temp.data.indexOf('ftp')<0&&
  15. temp.data.indexOf('@')<0)continue;
  16. span=dcu.createElement('span');
  17. str=temp.data.replace(//&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
  18. //str=sqMethod.encode(temp.data);
  19. str=str.replace(/(ht|f)tp(s|)/:////[/-/w.:]+(//[^ /n/r/'/"]+|)/gi,function(match){
  20. return '<a href="'+match+'" class="sqUrl">'+match+'</a>'; } );
  21. str=str.replace(/[/w.]+@[/w/-.]+(//[^ /n/r/'/"]+|)/gi,function(match){
  22. return '<a href="mailto:'+match+'" class="sqMail">'+match+'</a>'; } );
  23. span.innerHTML=str;
  24. obj.replaceChild(span, temp);
  25. }
  26. if(temp.nodeType==1)arguments.callee(obj.childNodes[i], dcu);
  27. } temp=span=null; // shawl.qiu script
  28. }
  29. //]]>
  30. </script>

2. append URL/email link to the string

 
 
  1. <script type="text/javascript">
  2. //<![CDATA[
  3. onload=function(){
  4. var html=document.body.innerHTML;
  5. document.body.innerHTML=fPadUrl(document.body.innerHTML);
  6. document.body.innerHTML=fPadEmail(document.body.innerHTML);
  7. }
  8. function fPadUrl(str){
  9. if(!str||str.replace(//s+/,'').length<0)return false;
  10. var re=new RegExp('([//s//S]|)((ht|f)t(p|ps)/:////.*?)(//s|/&nbsp/;|/&gt/;|/&lt/;|'+
  11. '[/'/"<>]|/&/#34/;)','gi');
  12. return str.replace(re,
  13. function($1,$2,$3,$4,$5,$6){
  14. if(//'|/"|/=|/= /.test($2))return $1;
  15. $3='<a href="'+$3+'" class="sqLink">'+$3+'</a>';
  16. return $2+$3+$6;
  17. }
  18. ); // shawl.qiu script
  19. }
  20. function fPadEmail(str){
  21. if(!str||str.replace(//s+/,'').length<0)return false;
  22. var re=new RegExp('(mailto/:|/>|)([//w/./-]+)/@(.*?)'+
  23. '(//s|/&nbsp/;|/&gt/;|/&lt/;|[/'/"<>]|/&/#34/;)','gi');
  24. return str.replace(re,
  25. function($1,$2,$3,$4,$5){
  26. if(/mailto|>/.test($2))return $1;
  27. $3='<a href="mailto:'+$3+'@'+$4+'" class="sqLink">'+$3+'@'+$4+'</a>';
  28. return $2+$3+$5;
  29. }
  30. ); // shawl.qiu script
  31. }
  32. //]]>
  33. </script>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.