Skip to content Skip to sidebar Skip to footer

History.pushstate() Change Query Values

If I have a link that is being changed with the function history.pushState({}, '', link); where my link is for example page.php?value=1&value2=2 Is there a way to just change t

Solution 1:

If what you're trying to do is change the URL without adding an additional entry to the history object, you might try replaceState.

history.replaceState({value: 1, value2: X}, "title", "page.php");

Solution 2:

No, because the query string is part of the URL. If you don't truly need to pass those values for the purposes of the server, you can include them in the history's state object itself, and then you can change just the state object with pushState(). For example:

history.pushState({value: 1, value2: 2}, "Title", 'page.php');
history.pushState({value: 1, value2: 'new value'}, "Title");

Solution 3:

You can use this useful function to change a query string parameter value:

functionupdateParam(url, param, value)
{  
    var re = newRegExp(param+"(.+?)(&|$)","g");

    return url.replace(re, param+'='+value)  
}

Post a Comment for "History.pushstate() Change Query Values"