Getting Query String Values In JavaScript

Getting Query String Values In JavaScript.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <title>Parsing URL Query String In Javascript</title>

    <script type="text/javascript">

    // Build an empty URL structure in which we will store
    // the individual query values by key.
    var objURL = new Object();

    // Use the String::replace method to iterate over each
    // name-value pair in the query string. Location.search
    // gives us the query string (if it exists).
    window.location.search.replace(
    new RegExp( "([^?=&]+)(=([^&]*))?", "g" ),

    // For each matched query string pair, add that
    // pair to the URL struct using the pre-equals
    // value as the key.
    function( $0, $1, $2, $3 ){
    objURL[ $1 ] = $3;
    }
    );

    </script>
    </head>
    <body>

    <p>
    <script type="text/javascript">

    // Loop over the URL values that we collected.
    for (var strKey in objURL){

    // Output the key to the document.
    document.write(
    "<strong>" + strKey + ":</strong> "
    );

    // Output the value stored at that key.
    document.write(
    objURL[ strKey ] + "<br />"
    );
    }

    </script>
    </p>

    <p>
    <strong>Database:</strong>
    <span id="database"></span>
    </p>

    <script type="text/javascript">

    // Now that the most of the document is loaded, we
    // can reach into the DOM and set values on specific
    // node elements. Let's set the Database node.
    var objNode = document.getElementById( "database" );

    // Check to make sure we have a valid node and that
    // our URL does indeed contain the proper variable.
    if (objNode && objURL[ "i" ]){

    // Set the inner HTML to reflect the value of
    // the database that was sent in the URL.
    objNode.innerHTML = objURL[ "i" ];

    }

    </script>

    </body>
    </html>
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to Getting Query String Values In JavaScript

  1. RG says:

    function OnDivScroll()
    {
    var content = document.getElementById(”);
    if(content.scrollTop < content.scrollHeight – 500)
    return;

    CountryWS.GetMoreCountriesObject(pageIndex, pageSize, HandleRetrievedData );
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s