Link to home
Start Free TrialLog in
Avatar of MayoorPatel
MayoorPatel

asked on

Grabbing the Querysting of a URL in Javascript

Hi there I have a javascript call to a reload on my page.

<script type="text/javascript">
parent.location.reload("finish.asp?UserId=")
</script>

I need to append finish.asp in the code above with the UserId from the previous page. I am passing the userId in the URL like this

Response.Redirect("finish3.asp?ID=" & intNewId)

Can anyone help please?
Avatar of dctuck
dctuck
Flag of United Kingdom of Great Britain and Northern Ireland image

You have to manually parse the window.location property to find any query string parameters. Alternatively, if you don't feel like writing all the extra code yourself, there are some javascript libraries already written to do so. Here's an example library:
http://andrewu.co.uk/tools/request/
 
I've just put together this very basic script - you just need to pass the Querystring parameter name you want to find the value for to the QueryString function, and it will return the value
function QueryString(item) {
    // Get the value of the key/value pair in the document URL
    var href = window.location.search;
    if (href == "") {
        return null;
    }
    href = href.substring(1);
    var items = href.split("&");
    for (var i = 0; i < items.length; i++) {
        var keyValue = items[i].split("=");
        if (keyValue[0] == item) {
            return keyValue[1];
        }
    }
    return null;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial