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

时间:2022-08-26 15:50:15

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>   

#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>