javascript “window.location.history.go(-2)”在主流浏览器中可行吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3811235/
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-25 02:20:44  来源:igfitidea点击:

"window.location.history.go(-2)" possible in major browsers?

javascripthtmlsecurityinternet-explorerbrowser

提问by bobince

As the title says, will this code work and will it work in major browsers?

正如标题所说,这段代码能用吗,能在主流浏览器上用吗?

I ask because currently I have no resources to test it, so I would appreciate some help on this.

我问是因为目前我没有资源来测试它,所以我很感激这方面的帮助。

Here is what I have (not tested):

这是我所拥有的(未测试):

setTimeout(window.location.history.go(-2), 5000);

Thanks

谢谢

回答by bobince

setTimeout(window.location.history.go(-2), 5000);

historyis a property of window, not location. Also if you want it to trigger after a delay you will need to make a delayed-call function—currently you are calling go()immediately, and passing the return value of the function to setTimeout, which clearly won't work. You probably mean:

history是 的属性window,不是location。此外,如果您希望它在延迟后触发,您将需要创建一个延迟调用函数——目前您正在go()立即调用,并将函数的返回值传递给setTimeout,这显然是行不通的。你可能的意思是:

setTimeout(function() {
    history.go(-2);
}, 5000);

As for ‘go back two pages', yes, it'll work in pretty much all JS-supporting browsers, but it's the kind of thing users are likely to find incredibly confusing. Are you sure you want to do that?

至于“返回两页”,是的,它几乎可以在所有支持 JS 的浏览器中工作,但用户可能会发现这种令人难以置信的混乱。您确定要这样做吗?

回答by oezi

as you can see herethis is supported by all browsers for a long time (since ff1.0 / opera 5 / ie 3).

正如您在此处看到的那样,所有浏览器长期以来都支持此功能(自 ff1.0/opera 5/ie 3 起)。

回答by Keenora Fluffball

It works on Netscape 2.0+, IE3+, Opera 5.12+, Firefox 1+, Konquerer 3.1+, Safari 1+. You just have to be sure, there are at least so many sites in the history, you want to go back.

它适用于 Netscape 2.0+、IE3+、Opera 5.12+、Firefox 1+、Konquerer 3.1+、Safari 1+。你只需要确定,历史上至少有这么多网站,你想回去。

German reference on SELFHTML

关于 SELFHTML 的德语参考

回答by Delan Azabani

It has been around since the first version of JavaScript, so it's universally supported. Please note, though, that your code will not work as it currently is because you're calling gonow, and passing the result of the function as the function reference. Also, it's just history, not location.history. Try this instead:

它自 JavaScript 的第一个版本以来就已经存在,因此受到普遍支持。但请注意,您的代码将无法正常工作,因为您正在调用gonow,并将函数的结果作为函数引用传递。此外,它只是history,不是location.history。试试这个:

setTimeout(function() { history.go(-2); }, 5000);