简体   繁体   English

javascript:history.go(-1)不适用于IE和Chrome

[英]javascript:history.go(-1) not working with IE and Chrome

I am trying the below code for traversing back to the previous page and it does not work on IE and Chrome but works fine on Edge. 我正在尝试下面的代码来遍历上一页,它不适用于IE和Chrome,但在Edge上可以正常工作。

<a href="javascript:history.go(-1)" class="back" >
            <asp:Label ID="backLabel" runat="server" Text="Back" meta:resourcekey="backLabelResource1" />
        </a>

Also tried this as per some threads I read 根据我阅读的一些线程也尝试了这个

<a href="javascript:void(0);" onclick="history.back(); return false;" class="back" >
            <asp:Label ID="backLabel" runat="server" Text="Back" meta:resourcekey="backLabelResource1" />
        </a>

Still same result. 结果还是一样。 Any ideas? 有任何想法吗?

There's no problem with history.go as both Chrome and IE support it . Chrome和IE都支持 history.go没问题。 This code does work: 此代码可以正常工作:

 document.getElementById("mylink").addEventListener("click", function() { alert("Hello world!") // Or alternatively: //history.go(-1); }); 
 <a href="javascript:history.go(-1)">Navigate 1 page backwards</a> <br /><br /> <a href="javascript:alert('Hello World!')">Announce "Hello World"</a> <br /><br /> <a href="javascript:alert('Hello World!')"> <label>This is a label wrapped in an a tag which is not recommended, and will not work on certain browsers such as IE (and for example in this specific case does not show the pointer cursor in Chrome)</label> </a> <br /><br /> <label> <a href="javascript:void(0)" id="mylink"> This is correct practice. No inline js, and the a tag is in the label tag and not the other way around (which only matters if you want to work with IE as other modern browsers allow this). </a> </label> 

The above works for me using Chrome 66 (latest). 以上适用于我使用Chrome 66(最新)的方法。 Perhaps try and see what ASP renders in the browser using inspect element and check if some of the code may be invalid. 也许尝试使用inspect元素查看ASP在浏览器中呈现的内容,并检查某些代码是否无效。 All the above solutions should work. 以上所有解决方案均应起作用。


Also, it is considered bad practice to use inline js. 另外,使用内联js被认为是不好的做法。 Read more: 阅读更多:

Try: 尝试:

window.history.back()

or 要么

window.history.forward()

Reference 'Traveling through history': https://developer.mozilla.org/en-US/docs/Web/API/History_API 参考“穿越历史”: https : //developer.mozilla.org/en-US/docs/Web/API/History_API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM