1

I have found plenty of ways to do this via PHP but on a simple HTML site I have run into a roadblock. And I'm not really strong on JS so that doesn't help.

Basically I am passing a very simple URL variable (a ref #) via a link (...com/Apply-Online?12345) and I need to load it into an input text field on a form in the linked page. Most of the scripts I have found want to parse out multiple variables and it gets really complicated in a hurry. Since I only have 1 value it seems like there should be an easy way to parse the variable from the URL and pass it into a var and pass that to the ID of the field.

I have the window.onload part figured out. I just need the code to parse out the variable.

window.onload = function(){
    document.getElementById("input_id").value = 12345;
};

3 Answers 3

2

It is as simple as:

document.getElementById("input_id").value = location.search.replace('?','');

Sign up to request clarification or add additional context in comments.

2 Comments

I knew it would be something simple! Works perfectly Martin. Thanks so much.
Anytime, feel free to accept the answer if it was what you need ;)
1

Assuming your link includes a variable name, such as ...com/Apply-Online?var1=12345:

<script type="text/javascript">
    function $_GET(q,s) { 
        s = s ? s : window.location.search; 
        var re = new RegExp('&'+q+'(?:=([^&]*))?(?=&|$)','i'); 
        return (s=s.replace(/^?/,'&').match(re)) ? (typeof s[1] == 'undefined' ? '' : decodeURIComponent(s[1])) : undefined; 
    }

    window.onload = function(){
        document.getElementById("input_id").value = $_GET('var1');
    };
</script>

Source: http://www.onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/

Comments

0
var value = document.location.search;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.