05 March 2008

JavaScript Url Parser

Parsing the URL with JavaScript is a tedious task and can spend a lot of time.

A simple JavaScript library is the solution.

Poly9's Polyvalent JavaScript URL Parser is a complete URL parser that extracts the information from complex URLs:

http://user:password@WebServer.com/extension?argument1=value1#fragment

Get de JS File here.
The parser doesn't return the virtual directory.
A possible solution is to create a custom library and add the following functions:


// url format http://server/vdir/page.aspx?QueryStringParams
function GetVirtualDirectory(url) 
{ 
    var urlParts = url.split("/");     
    if (EndsWith(urlParts[3], ".aspx"))
    {
        return "";
    }
    
    return urlParts[3] + "/";    
} 
 


function EndsWith(str, end)
{
    var reg = new RegExp (end + "$");
    return reg.test(str);
}

No comments: