09/15/2009
Useful Javascript functions
Posted by
Andre Small
The String.Trim(), String.StartWith and String.EndWith methods are very useful methods that are prebuilt into the .NET framework, that need a client-side implementation. Below are the javascript equivalents that I have found to be extremely helpful.
String.prototype.trim = function(){return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))}
String.prototype.startsWith = function(str) {return (this.match("^"+str)==str)}
String.prototype.endsWith = function(str) {return (this.match(str+"$")==str)}
Credit: http://www.tek-tips.com/faqs.cfm?fid=6620
Also, The ResolveUrl() method that I (we) use a lot in ASP.NET development. Here is a Javascript equivalent. Note: This requires some help from ASP.NET to provide the resolution of the "~/" and needs to be inline in the HTML.
function ResolveUrl(url) {
if (url.match("^~/") == "~/") {
return url.replace("~/", "<%= ResolveUrl("~/") %>");
}
return url;
}
I hope this helps you.
« Back to Blog Main Page
|