AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=160&pId=-1
Useful JavaScript String Functions
page
by Taewook Kang
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 19935/ 30

Clickify

Here are some of the string functions I have written. They can be used either client-side or server-side, although smartRmHTML would need the server.HTMLEncode method removed if it were used client-side. Enjoy!

function autoHTML( strSrc,optAtt ) {
 //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 // autoHTML function
 // author: T. Kang
 // contact: txkang@hotmail.com
 // This function will make http,https,ftp,www and email strings clickable.
 //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
 
 strSrc = strSrc.replace( /\s(https:\/\/|http:\/\/|ftp:\/\/|www.)([^\s]*)/gi,
   ' <a href=http://$2 ' + optAtt + '>$1$2</a>' );
 strSrc = strSrc.replace( /\s([^\s]*@[^\s]*)/gi,' <a href=mailto:$1>$1</a>' );
 return strSrc;
}
Strip HTML Tags

function smartRmHTML( strSrc ) {
//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// smartRmHTML function
// author: T. Kang
// contact: txkang@hotmail.com
// Strip off HTML tags. Better than other removeHtml functions out there.
//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

return server.HTMLEncode( strSrc.replace( /<[^<|>]+?>/gi,'' ) );
}

Extract Links

 function listAllLinks( strSrc,blnClickable,optAtt ) {
//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// listAllLinks function
// author: T. Kang
// contact: txkang@hotmail.com
// Extract every link in string and list them in order.
//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
var arrAllLinks

if ( !blnClickable ) {
arrAllLinks = strSrc.match(/(https:\/\/|http:\/\/|ftp:\/\/|www.)([^\s]*)/gi );
return arrAllLinks.join( ',' );
}
else {
arrAllLinks = strSrc.match(/(https:\/\/|http:\/\/|ftp:\/\/|www.)([^\s]*)/gi );
return arrAllLinks.join( ',' ).replace( /(https:\/\/|http:\/\/|ftp:\/\/|www.)([^,]*)/gi,
'<a href=http://$2 ' + optAtt + '>$1$2</a>' );
}
}


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 6:51:35 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search