views:

586

answers:

5

I need to parse the query string www.mysite.com/default.aspx?dest=aboutus.aspx. How do I get the dest variable in JavaScript?

+2  A: 

You need to use the location.search property.

Here is some sample code to parse it.

SLaks
could u write the code snippet here? i am very new to this.it should be likeif q1 = "?dest" then window.open "/newpage.aspx"thats all i need. thanks
sinaw
That link's a 404.
Gert G
A: 

Here's a script that might help: http://adamv.com/dev/javascript/querystring
Basically you end up parsing document.location.

eliah
A: 

Here is a fast and easy way of parsing QueryString in Javascript.

<script>
    function getQueryVariable(variable) {
        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 (pair[0] == variable) {
                return pair[1];
            }
        }
        alert('Query Variable ' + variable + ' not found');
    }
</script>

Now make a request to page.html?x=Hello

<script>
    alert(getQueryVariable("x"));
</script>

Source : http://tinyurl.com/4yfr28

Braveyard
you should also decode any special characters that have been percent-encoded
A: 

Use the parseQueryString.js Javascript function found here:

safalra.com/web-design/javascript/parsing-query-strings/

In your example, if you wanted to test the parsing of the "dest" variable for the URL: www.mysite.com/default.aspx?dest=aboutus.aspx then you can try: http://urlparser.com/?url=www.mysite.com/default.aspx?dest=aboutus.aspx

URLParser.com
A: 

Have a look at this solution. Using his function, you would just not to call gup('dest') to grab the URL dest parameter.

Gert G