javascript history.go('url') 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6277283/
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
history.go('url') issue
提问by Vikas
I've seen that history.go() method can have two types of parameter:
我已经看到 history.go() 方法可以有两种类型的参数:
see:
看:
http://www.w3schools.com/jsref/met_his_go.asp
But url does not working at all.
但是 url 根本不起作用。
I use JavaScript console of browser to test this method but got no success.
我使用浏览器的 JavaScript 控制台来测试此方法,但没有成功。
I know that there are security issues, that you can't read history entries. You can just go back and forward.
我知道存在安全问题,您无法读取历史记录。你可以来回走动。
So why this string parameter listed in all js references?
那么为什么在所有 js 引用中都列出了这个字符串参数呢?
回答by Andy E
Supplying a URL as a parameter is a non-standard feature and will not work in all browsers. Most browsers accept only a relative number, e.g. 1
or -1
.
提供 URL 作为参数是一项非标准功能,不适用于所有浏览器。大多数浏览器只接受一个相对数字,例如1
或-1
。
From the MDC documentation(emphasis mine):
从MDC 文档(强调我的):
[
history.go(integerDelta)
] Loads a page from the session history, identified by its relative location to the current page, for example -1 for the previous page or 1 for the next page. WhenintegerDelta
is out of bounds (e.g. -1 when there are no previously visited pages in the session history), the method doesn't do anything and doesn't raise an exception. Callinggo()
without parameters or with a non-integer argument has no effect (unlike Internet Explorer, which supports string URLs as the argument).
[
history.go(integerDelta)
] 从会话历史中加载一个页面,由其与当前页面的相对位置标识,例如 -1 表示上一页或 1 表示下一页。当integerDelta
越界时(例如,当会话历史记录中没有以前访问过的页面时为 -1),该方法不执行任何操作并且不会引发异常。调用go()
无参数或具有非整数参数没有影响(不像IE浏览器,它支持字符串的URL作为参数)。
This is what happens when you use W3Schoolsas a learning resource ;-)
当您使用W3Schools作为学习资源时会发生这种情况;-)
回答by Shadikka
回答by Chris Laarman
If you want to use it with an URL just use:
如果您想将它与 URL 一起使用,只需使用:
window.location.href="http://www.whereyouwannago.nl/";
The URL is problably still there for backwards compatibility
为了向后兼容,该 URL 可能仍然存在