onclick =“javascript:history.go(-1)”在Chrome中無效

[英]onclick=“javascript:history.go(-1)” not working in Chrome


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?

有什么問題?

8 个解决方案

#1


52  

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>

#2


6  

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>

#3


5  

Try this:

嘗試這個:

<a href="www.mypage.com" onclick="history.go(-1); return false;"> Link </a>

#4


4  

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?

addEventListener is the way to register an event listener as specified in W3C DOM. Its benefits are as follows:

addEventListener是注冊W3C DOM中指定的事件偵聽器的方法。它的好處如下:

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.

它允許為事件添加多個處理程序。這對於需要運行良好的DHTML庫或Mozilla擴展特別有用,即使使用其他庫/擴展也是如此。當偵聽器被激活時(捕獲與冒泡),它可以讓您對階段進行更精細的控制。它適用於任何DOM元素,而不僅僅是HTML元素。

<a id="back" href="www.mypage.com"> Link </a>

document.getElementById("back").addEventListener("click", window.history.back, false);

On jsfiddle

在jsfiddle

#5


1  

Try this dude,

試試這個家伙,

<button onclick="goBack()">Go Back 2 Pages</button>
<script>
  function goBack() {
    window.history.go(-2);
  }
</script>

#6


0  

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>

#7


0  

It worked for me. No problems on using javascript:history.go(-1) on Google Chrome.

它對我有用。在Google Chrome上使用javascript:history.go(-1)沒問題。

  1. To use it, ensure that you should have history on that tab.
  2. 要使用它,請確保您應該在該選項卡上有歷史記錄。
  3. Add javascript:history.go(-1) on the enter URL space.
  4. 在輸入網址空間添加javascript:history.go(-1)。
  5. It shall work for a few seconds.
  6. 它應該工作幾秒鍾。

#8


-1  

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 ......等),並且需要返回false以防止出現默認事件。

For example :

例如 :

<a href="#" onclick="window.history.go(-1); return false;"> Link </a>   

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/04/27/baa99f5003ba3e787761628891cac7e.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com