Variable from url to textbox?

Discussion in 'JavaScript' started by SuPrAiCeR69, Jun 10, 2009.

  1. #1
    If someone goes to the URL;

    http://www.mywebsite.com/index.html?name=jason

    I then want the textbox with ID 'name' to have the "jason" text automatically in there.

    What is the best way to go about it and if you have an example that would be great. Any idea what this process is called exactly...passing a string from url?

    Thanks!
     
    SuPrAiCeR69, Jun 10, 2009 IP
  2. Unni krishnan

    Unni krishnan Peon

    Messages:
    237
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Use this function on the onLoad event in the body tag
    Hope this helps.
     
    Unni krishnan, Jun 10, 2009 IP
  3. SuPrAiCeR69

    SuPrAiCeR69 Peon

    Messages:
    216
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks Unni krishnan

    Is there any way without a body onload? Like just in JS?
    Thanks again..much appreciated!
     
    SuPrAiCeR69, Jun 11, 2009 IP
  4. clarky_y2k3

    clarky_y2k3 Well-Known Member

    Messages:
    114
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    108
    #4
    You can use window.onload:-
    window.onload = loadName;
    Code (markup):
     
    clarky_y2k3, Jun 11, 2009 IP
  5. SuPrAiCeR69

    SuPrAiCeR69 Peon

    Messages:
    216
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks clarky_y2k3

    Tried both ways but it does not input the url text from after the "=" into the textbox.
    Code I have is as follows;

    
    <body onLoad="capture">
    
    <SCRIPT LANGUAGE="javascript">
    function capture()
    {
    var urlValue = window.location;
    var urlText = urlValue.toString();
    var start = urlText.indexOf("=") + 1;
    var nameText = urlText.substring(start, urlText.length);
    document.getElementById('name').value = nameText;
    }
    </SCRIPT>
    
    <form>
        <div class="div_texbox">
        <input name="name" class="name" id="name" value="" />
        </div>
    </form>
    </body>
    
    Code (markup):
     
    SuPrAiCeR69, Jun 11, 2009 IP
  6. Unni krishnan

    Unni krishnan Peon

    Messages:
    237
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    0
    #6
    Try writing the script portion in the HEAD tag.

    If you don't want it on the onLoad event try writing the code that Clarky specified after the 'capture' function in the script tags.

    Hope this will work fine.
     
    Unni krishnan, Jun 11, 2009 IP
  7. emed

    emed Peon

    Messages:
    70
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #7
    you have to add () here:
    <body onload="capture[B]()[/B]">
    Code (markup):
    or put
    window.onload = capture
    Code (markup):
    before </script>
     
    emed, Jun 11, 2009 IP