Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2007
    Thanked 0 Times in 0 Posts

    parsing a URL to pull a single item and place it into a form field

    Hello all.

    I have created a snippet of javascript code that will do the following:

    1. read the window URL
    2. depending on the window URL, a word will be placed into a hidden form field.
    3. Person submits form...etc., etc.

    Here is what I have.

    Let's say that I have a single landing page with a simple form on it. I would like to reference the referred URL by pulling a part of the window URL into a hidden field of the form. Let's say the three URLs are:


    I have created a javascript that takes the exact URL and check for equality. If the URL is equal, it will place the corresponding id into the hidden form field. See my code below.

    <script type="text/javascript">
    var lead_source = window.location.href
    if (lead_source == "http://www.whatever.com/index.htm?id=ref1")
       document.writeln("<input type=hidden name='lead_source' id='lead_source' value='ref1'>");
    else if (lead_source == "http://www.whatever.com/index.htm?id=ref2")
       document.writeln("<input type=hidden name='lead_source' id='lead_source' value='ref2'>");
    else if (lead_source == "http://www.whatever.com/index.htm?id=ref3")
       document.writeln("<input type=hidden name='lead_source' id='lead_source' value='ref3'>");
    What I am looking to do is modify the code to only look at the "id" variable, not the entire URL. That way, if I add any additional items to the URL I will not have to change the code. I guess I am looking for an extra step of parsing the URL...maybe.

    Thanks for your help

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Thanked 4,738 Times in 4,700 Posts
    // code to parse query string
    var querystring = [ ];
    if ( location.search.length > 1 )
        var pairs = location.search.substring(1).split("&");
        for ( var p = 0; p < pairs.length; ++p ) 
            var pair = pairs[p];
            var name = pair[0];
            var value = unescape( pair[1].replace(/\+/g, " ") );
            querystring[name] = value;
    Once you have done that, you can use
          var theIDpassed = querystring["id"]
    to get the value of the id=xxx. Note that this also gets a name/value entry in the querystring variable for any pair seen in the url after the question mark.

    For a more complete answer, for filling in form fields based on the querystring and dependant on the *types* of those fields, look at post #6 in this old thread:
    Advanced Javascript - Prepopulate fields using URL
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts