javascript 页面加载时如何转到 history.back() ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4014115/
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
How can I go to history.back() when page loads?
提问by sebastian
I have a PHP page that has an error, but if I go back, it works fine. I can't redirect because the page can be accessed from two different places.
我有一个有错误的 PHP 页面,但如果我返回,它工作正常。我无法重定向,因为可以从两个不同的地方访问该页面。
How can I make a JavaScript that loads automatically when the page loads?
如何制作在页面加载时自动加载的 JavaScript?
Thanks, Sebastian
谢谢,塞巴斯蒂安
EDIT
编辑
that wouldn't work. is there a way to get the url of history.go(-2)?
那行不通。有没有办法获取history.go(-2)的url?
thanks, Sebastian
谢谢,塞巴斯蒂安
采纳答案by Vytautas
...
<body onload="history.go(-1);">
...
回答by Eric
The problem with using the window's onload property is that you do not want the user to wait for the current page to load. Just put this code/HTML at the top of the page right after the opening <head>tag.
使用窗口的 onload 属性的问题是您不希望用户等待当前页面加载。只需将此代码/ HTML 放在页面顶部的开始<head>标记之后即可。
<script type="text/javascript">history.go(-1);</script>
回答by Nick Craver
You can use the window.historyobject, like this:
您可以使用window.historyobject,如下所示:
history.back();
//or:
history.go(-1);

