0

How can i get querstring from url.

Example :

http://www.bla.com/lp/index.html?ref=test

I want to get the "test" to hidden input that will send on form that i have (the form works great, all i need to know is how to get the value "test").

Javascript? Jquery?

I tried something funny i guess, without knowledge:

<script type="text/javascript">
     function getQueryString() {
            var query_string = {};
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i=0;i<vars.length;i++) {
                var pair = vars[i].split("=");
                if (typeof query_string[pair[0]] === "undefined") {
                    query_string[pair[0]] = pair[1];
                } else if (typeof query_string[pair[0]] === "string") {
                    var arr = [ query_string[pair[0]], pair[1] ];
                    query_string[pair[0]] = arr;
                } else {
                    query_string[pair[0]].push(pair[1]);
                }
            } 
            return query_string;
        }
var queryString = getQueryString();

document.getElementById('new_leadsourcetext').value = queryString.ref; //myhiddeninput is  id of inpu
    </script>

<input type="hidden"  name="new_leadsourcetext" ID="new_leadsourcetext" /> 
1

1 Answer 1

1

Check this out. Call the getQueryString() function which would return an object with key and value pair.

    function getQueryString() {
                var query_string = {};
                var query = window.location.search.substring(1);
                var vars = query.split("&");
                for (var i=0;i<vars.length;i++) {
                    var pair = vars[i].split("=");
                    if (typeof query_string[pair[0]] === "undefined") {
                        query_string[pair[0]] = pair[1];
                    } else if (typeof query_string[pair[0]] === "string") {
                        var arr = [ query_string[pair[0]], pair[1] ];
                        query_string[pair[0]] = arr;
                    } else {
                        query_string[pair[0]].push(pair[1]);
                    }
                } 
                return query_string;
            }
    var queryString = getQueryString();

    document.getElementById('myhiddeninput').value = queryString.ref; //myhiddeninput is  id of input
Sign up to request clarification or add additional context in comments.

3 Comments

thanks. how i call the function? just value="get..." ?
A potentially dangerous Request.Form value was detected from the client (new_leadsourcetext="<script>getQueryStri...").
Set an ID (myhiddeninput) to the hidden input and use the last line to set the hidden input value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.