compile-fan compile-fan - 1 year ago 90
Javascript Question

How to retrieve GET parameters from javascript?

,how can it retrieve

For the above simple example,
should be

But it should also work for complex querystrngs...

Answer Source

With the window.location object. This code gives you GET without the question mark."?", "");

From your example it will return returnurl=%2Fadmin

EDIT: I took the liberty of changing Qwerty's answer, which is really good, and as he pointed I followed exactly what the OP asked:

function parse(val) {
    var result = "Not found",
        tmp = [];
    //.replace ( "?", "" ) 
    // this is better, there might be a question mark inside
        .forEach(function (item) {
        tmp = item.split("=");
        if (tmp[0] === val) result = decodeURIComponent(tmp[1]);
    return result;

I removed the duplicated function execution from his code, replacing it a variable ( tmp ) and also I've added decodeURIComponent, exactly as OP asked. I'm not sure if this may or may not be a security issue.

Or otherwise with plain for cycle, which will work even in IE8:

function parseSecond(val) {
    var result = "Not found",
        tmp = [];
    var items ="&");
    for (var index = 0; index < items.length; index++) {
        tmp = items[index].split("=");
        if (tmp[0] === val) result = decodeURIComponent(tmp[1]);
    return result;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download