Return to Snippet

Revision: 11690
at February 14, 2009 17:48 by jrharshath


Updated Code
/*
 * Javascript URL Parser
 * @author jrharshath (http://jrharshath.wordpress.com/)
 * @description Parses any URL like string and returns an array or URL "Components"
 * 
 * Working demo located at http://jrharshath.qupis.com/urlparser/
 * While using in a webapp, use "urlparse(window.location.href)" to parse the current location of the web page
 * Free to use, just provide credits.
 */
function urlparse( str )
{
    var arr = str.split('#');

    var result = new Array();
    var ctr=0;
    for each( part in arr )
    {
        var qindex = part.indexOf('?');
        result[ctr] = {};
        if( qindex==-1 )
        {
            result[ctr].mid=part;
            result[ctr].args = [];
            ctr++;
            continue;
        }
        result[ctr].mid = part.substring(0,qindex);
        var args = part.substring(qindex+1);
        args = args.split('&');
        var localctr = 0;
        result[ctr].args = new Array();
        for each( val in args )
        {
            var keyval = val.split('=');
            result[ctr].args[localctr] = new Object();
            result[ctr].args[localctr].key = keyval[0];
            result[ctr].args[localctr].value = keyval[1];
            localctr++;
        }
        ctr++;
    }
    return result;
}

Revision: 11689
at February 14, 2009 17:37 by jrharshath


Updated Code
/*
 * Javascript URL Parser
 * @author jrharshath (http://jrharshath.wordpress.com/)
 * @description Parses any URL like string and returns an array or URL "Components"
 * 
 * Working demo located at http://jrharshath.qupis.com/urlparser/
 * Free to use, just provide credits.
 */
function urlparse( str )
{
    var arr = str.split('#');

    var result = new Array();
    var ctr=0;
    for each( part in arr )
    {
        var qindex = part.indexOf('?');
        result[ctr] = {};
        if( qindex==-1 )
        {
            result[ctr].mid=part;
            result[ctr].args = [];
            ctr++;
            continue;
        }
        result[ctr].mid = part.substring(0,qindex);
        var args = part.substring(qindex+1);
        args = args.split('&');
        var localctr = 0;
        result[ctr].args = new Array();
        for each( val in args )
        {
            var keyval = val.split('=');
            result[ctr].args[localctr] = new Object();
            result[ctr].args[localctr].key = keyval[0];
            result[ctr].args[localctr].value = keyval[1];
            localctr++;
        }
        ctr++;
    }
    return result;
}

Revision: 11688
at February 14, 2009 17:32 by jrharshath


Updated Code
/*
 * Javascript URL Parser
 * @author jrharshath (http://jrharshath.wordpress.com/)
 * @description Parses any URL like string and returns an array or URL "Components"
 * 
 * Working demo located at http://jrharshath.qupis.com/urlparser/
 * Free to use, just provide credits.
 */
function urlparse( str )
{
    //str = str.substring( str.indexOf('#')+1 );
    var arr = str.split('#');

    var result = new Array();
    var ctr=0;
    for each( part in arr )
    {
        var qindex = part.indexOf('?');
        result[ctr] = {};
        if( qindex==-1 )
        {
            result[ctr].mid=part;
            result[ctr].args = [];
            ctr++;
            continue;
        }
        result[ctr].mid = part.substring(0,qindex);
        var args = part.substring(qindex+1);
        args = args.split('&');
        var localctr = 0;
        result[ctr].args = new Array();
        for each( val in args )
        {
            var keyval = val.split('=');
            result[ctr].args[localctr] = new Object();
            result[ctr].args[localctr].key = keyval[0];
            result[ctr].args[localctr].value = keyval[1];
            localctr++;
        }
        ctr++;
    }
    return result;
}

Revision: 11687
at February 14, 2009 17:31 by jrharshath


Updated Code
/*
 * Javascript URL Parser
 * @author jrharshath (http://jrharshath.wordpress.com/)
 * @description Parses any URL like string and returns an array or URL "Components"
 * 
 * Free to use, just provide credits.
 */
function urlparse( str )
{
    //str = str.substring( str.indexOf('#')+1 );
    var arr = str.split('#');

    var result = new Array();
    var ctr=0;
    for each( part in arr )
    {
        var qindex = part.indexOf('?');
        result[ctr] = {};
        if( qindex==-1 )
        {
            result[ctr].mid=part;
            result[ctr].args = [];
            ctr++;
            continue;
        }
        result[ctr].mid = part.substring(0,qindex);
        var args = part.substring(qindex+1);
        args = args.split('&');
        var localctr = 0;
        result[ctr].args = new Array();
        for each( val in args )
        {
            var keyval = val.split('=');
            result[ctr].args[localctr] = new Object();
            result[ctr].args[localctr].key = keyval[0];
            result[ctr].args[localctr].value = keyval[1];
            localctr++;
        }
        ctr++;
    }
    return result;
}

Revision: 11686
at February 14, 2009 16:54 by jrharshath


Initial Code
function urlparse( str )
{
    //str = str.substring( str.indexOf('#')+1 );
    var arr = str.split('#');

    var result = new Array();
    var ctr=0;
    for each( part in arr )
    {
        var qindex = part.indexOf('?');
        result[ctr] = {};
        if( qindex==-1 )
        {
            result[ctr].mid=part;
            result[ctr].args = [];
            ctr++;
            continue;
        }
        result[ctr].mid = part.substring(0,qindex);
        var args = part.substring(qindex+1);
        args = args.split('&');
        var localctr = 0;
        result[ctr].args = new Array();
        for each( val in args )
        {
            var keyval = val.split('=');
            result[ctr].args[localctr] = new Object();
            result[ctr].args[localctr].key = keyval[0];
            result[ctr].args[localctr].value = keyval[1];
            localctr++;
        }
        ctr++;
    }
    return result;
}

Initial URL
http://jrharshath.wordpress.com/

Initial Description
This is a script that can parse a URL string, and return all components in a systematic way via an object.
This script is very useful in places where you want to link to a page "dynamically" generated by javascript. This is usually done by adding GET-like params after a # in the URL.

This parser can parse URLs like:

    * http://jrharshath.com/
    * http://jrharshath.com/page?param=value
    * http://jrharshath.com/page?param=value&param2=value2
    * http://jrharshath.com/some?serverparam=somevalue#JSFeature?param=val&param2=val2
    * http://jrharshath.com/some?serverparam=somevalue#AjaxPage?param=val&param2=val2#AnotherAjaxFeature?featureparam=featureval

Initial Title
Javascript URL Parser

Initial Tags
url, javascript

Initial Language
JavaScript