onclick="javascript:history.go(-1)" 在 Chrome 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16253035/
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
onclick="javascript:history.go(-1)" not working in Chrome
提问by user961627
This code works fine in FF, it takes the user back to the previous page, but not in Chrome:
这段代码在 FF 中工作正常,它会将用户带回上一页,但在 Chrome 中则不然:
<a href="www.mypage.com" onclick="javascript:history.go(-1)"> Link </a>
What's the fix?
有什么解决办法?
回答by Mohit Padalia
You should use window.history
and return a false so that the href
is not navigated by the browser ( the default behavior ).
您应该使用window.history
并返回 false 以便href
浏览器不会导航(默认行为)。
<a href="www.mypage.com" onclick="window.history.go(-1); return false;"> Link </a>
回答by Majid Ali Khan
Use the below one, it's way better than the history.go(-1)
.
使用下面的,它比history.go(-1)
.
<a href="#" onclick="location.href = document.referrer; return false;"> Go TO Previous Page</a>
回答by karaxuna
Try this:
试试这个:
<a href="www.mypage.com" onclick="history.go(-1); return false;"> Link </a>
回答by Xotic750
Why not get rid of the inline javascript and do something like this instead?
为什么不摆脱内联 javascript 而做这样的事情呢?
Inline javascript is considered bad practice as it is outdated.
内联 javascript 被认为是不好的做法,因为它已经过时了。
Notes
Why use addEventListener?
addEventListener is the way to register an event listener as specified in W3C DOM. Its benefits are as follows:
It allows adding more than a single handler for an event. This is particularly useful for DHTML libraries or Mozilla extensions that need to work well even if other libraries/extensions are used. It gives you finer-grained control of the phase when the listener gets activated (capturing vs. bubbling) It works on any DOM element, not just HTML elements.
笔记
为什么使用addEventListener?
addEventListener 是注册 W3C DOM 中指定的事件侦听器的方法。它的好处如下:
它允许为一个事件添加多个处理程序。这对于即使使用其他库/扩展也需要正常工作的 DHTML 库或 Mozilla 扩展特别有用。当侦听器被激活(捕获与冒泡)时,它可以让您对阶段进行更细粒度的控制。它适用于任何 DOM 元素,而不仅仅是 HTML 元素。
<a id="back" href="www.mypage.com"> Link </a>
document.getElementById("back").addEventListener("click", window.history.back, false);
On jsfiddle
回答by Rahmad Saleh
Try this dude,
试试这个哥们
<button onclick="goBack()">Go Back 2 Pages</button>
<script>
function goBack() {
window.history.go(-2);
}
</script>
回答by StoodyHarp72317
It worked for me. No problems on using javascript:history.go(-1)
on Google Chrome.
它对我有用。javascript:history.go(-1)
在Google Chrome上使用没有问题。
- To use it, ensure that you should have history on that tab.
- Add
javascript:history.go(-1)
on the enter URLspace. - It shall work for a few seconds.
- 要使用它,请确保该选项卡上应该有历史记录。
- 添加
javascript:history.go(-1)
上输入URL的空间。 - 它会工作几秒钟。
回答by Sumeet_Pol
javascript:history.go(-1);
javascript:history.go(-1);
was used in the older browser.IE6. For other browser compatibility try
用于旧版浏览器 IE6。对于其他浏览器兼容性尝试
window.history.go(-1);
where -1 represent the number of pages you want to go back (-1,-2...etc) and
return false
is required to prevent default event.
其中 -1 表示您要返回的页数(-1、-2...等)并且
return false
需要防止默认事件。
For example :
例如 :
<a href="#" onclick="window.history.go(-1); return false;"> Link </a>
回答by Deepak Pandey
Use Simply this line code, there is no need to put anything in href attribute:
使用简单的这一行代码,不需要在 href 属性中放任何东西:
<a href="" onclick="window.history.go(-1)"> Go TO Previous Page</a>