javascript 用js函数window.history.back()添加参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19051212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:10:59  来源:igfitidea点击:

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

javascriptfunctionurlinputparameters

提问by GabrieleU

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?

如何使用此 javascript 函数添加 url 参数以在后页返回,例如“index=1”?

采纳答案by Paul Rad

Like this :

像这样 :

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

回答by Royi Namir

You can use cookies :

您可以使用 cookie:

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

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

回答by André Dion

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

使用window.history您可以使用修改用户的当前历史记录window.history.replaceState(),但请注意,此方法仅适用于现代浏览器:

  • Chrome 5+
  • Firefox 4+
  • IE 10+
  • Opera 11.5+
  • Safari 5+
  • 铬 5+
  • 火狐 4+
  • 浏览器 10+
  • 歌剧 11.5+
  • 野生动物园 5+

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

对于更向后兼容的解决方案,您应该window.history完全避免,而是使用window.location,设置显式 URL 而不是依赖于用户的历史记录。