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

    parse a URL and place item into hidden 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:

    http://www.whatever.com/index.htm?id=ref1
    http://www.whatever.com/index.htm?id=ref2
    http://www.whatever.com/index.htm?id=ref3

    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.

    Code:
    <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'>");
       }
    }
    </script>
    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! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    The Apostate Apostropartheid's Avatar
    Join Date
    Oct 2007
    Posts
    3,215
    Thanks
    16
    Thanked 265 Times in 263 Posts
    Although...don't. You shouldn't use JavaScript for this. Using a server-side language such as PHP makes this much more accessible to people with it disabled.
    Last edited by Apostropartheid; 07-06-2010 at 11:29 PM.


  •  

    Posting Permissions

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