add a parameter with js function window.history.back()

20,819

Solution 1

Like this :

document.getElementById('backbutton').addEventListener('click', function() {
  window.location = document.referrer + '?index=1';
}, false);

Solution 2

You can use cookies :

<input id="backbutton" type="button" value="Back" onClick="runMe()"/>

function runMe()
{
 document.cookie = name+"=index%3d1; expires=whenever;path=/";
 window.history.back()
}

Solution 3

Using window.history you can modify the user's current history with window.history.replaceState(), but note that this method is only available in modern browsers:

  • Chrome 5+
  • Firefox 4+
  • IE 10+
  • Opera 11.5+
  • Safari 5+

For a more backwards compatible solution, you should avoid window.history altogether and instead use window.location, setting an explicit URL instead of relying on the user's history.

Share:
20,819
GabrieleU
Author by

GabrieleU

Updated on March 09, 2020

Comments

  • GabrieleU
    GabrieleU about 4 years

    I've this button:

    <input id="backbutton" type="button" value="Back" onClick="javascript:window.history.back()"/>
    

    how I could add url parameters to return in back page, like "index=1", with this javascript function?