0

If I have the following: http://www.domain.com/myscript?x1=v1&x2=v2

And I would like to reload this page with additional data {x3: v3, x4: v4}. What is the simple way to do without manipulating the original url?

usermark8
  • 35
  • 4

1 Answers1

0

Reload the page using window.location

var original_url = window.location;
window.location = original_url + "&x3=data3&x4=data4";

Edit: You can update values from url using this function:

/**
 * http://stackoverflow.com/a/10997390/11236
 */
function updateURLParameter(url, param, paramVal){
    var newAdditionalURL = "";
    var tempArray = url.split("?");
    var baseURL = tempArray[0];
    var additionalURL = tempArray[1];
    var temp = "";
    if (additionalURL) {
        tempArray = additionalURL.split("&");
        for (i=0; i<tempArray.length; i++){
            if(tempArray[i].split('=')[0] != param){
                newAdditionalURL += temp + tempArray[i];
                temp = "&";
            }
        }
    }

    var rows_txt = temp + "" + param + "=" + paramVal;
    return baseURL + "?" + newAdditionalURL + rows_txt;
}

You can use it:

var x1 = updateURLParameter(window.location.href, 'x1', 'updatedData1');
var url = updateURLParameter(x1, 'x2', 'updatedData2');

window.location = url;
aksu
  • 5,221
  • 5
  • 24
  • 39
  • Could you use a variable to represent the original url, something like original_url in your answer? – usermark8 Jan 24 '14 at 17:45
  • If tried to load the pages with different values more than once, these x3 and x4 with new values would all append to the url, could you please solve this? So I am trying to find a way to keep original url unchanged, each time refresh the page with just the new data. Otherwise the url would get longer and longer. – usermark8 Jan 24 '14 at 18:04
  • I added function to help your problem, is this what you're looking for? – aksu Jan 24 '14 at 18:19