Friday, October 5, 2012

Update an item of a querystring using Jquery

function updateQuerystring(str, itemToReplace, value) {
    if (str != null) {
        var pairs = str.split('&');
        $.each(pairs, function (i, v) {
            if (v.indexOf(itemToReplace) >= 0) {
                pairs[i] = itemToReplace + "=" + value;
            }
        });
        str = '';
        $.each(pairs, function (i, v) {
            if (pairs[i].indexOf("?") >= 0)
                str = pairs[i];
            else
                str = str.concat("&" + pairs[i]);
        });
    }

    return str;
}

No comments:

Post a Comment