简体   繁体   English

使用javascript history.back()在Safari中失败..如何让它跨浏览器?

[英]Using javascript history.back() fails in Safari .. how do I make it cross-browser?

I am using 我在用

<a href="index.php" onclick="history.back();return false;">Back</a>

to provide a back to previous page link. 提供返回上一页链接。 It works fine on Windows (IE/Mozilla) but fails in Safari on both Windows/Mac. 它在Windows(IE / Mozilla)上运行良好,但在Windows / Mac上都无法在Safari中运行。

Is there a way to make it work on all of the systems/browsers (cross-browser/platform)? 有没有办法让它适用于所有系统/浏览器(跨浏览器/平台)?

If it's not possible, is there any other way using PHP etc? 如果不可能,有没有其他方式使用PHP等?

it should be history.go(-1); return false; 它应该是history.go(-1); return false; history.go(-1); return false; or history.go(-1); event.preventDefault(); history.go(-1); event.preventDefault(); history.go(-1); event.preventDefault();

您应该考虑这样做:

<a href="javascript:history.go(-1)">Back</a>

Try this instead. 试试这个。 It should work across IE, FF, Safari and Chrome. 它应该适用于IE,FF,Safari和Chrome。

<a href="#" onclick="if(document.referrer) {window.open(document.referrer,'_self');} else {history.go(-1);} return false;">Cancel<a>

以下代码正常运行。

 <a href="javascript:void(0);" onclick="javascript:history.go(-1);">Back</a> 

If anyone is still struggling with this issue try removing the href -attribute from the link you want to use window.history.back() with. 如果有人还在努力解决这个问题,请尝试从你想要使用的链接中删除href -attribute window.history.back() I'm not sure if this workaround complies with HTML-standards but it worked out fine for me. 我不确定这种解决方法是否符合HTML标准,但对我来说效果很好。

I faced a similar issue on an e-commerce site I have been building for one of my customers. 我在为我的一个客户建立的电子商务网站上遇到了类似的问题。 I initially went the route of calling: 我最初走的是打电话:

window.history.back();

when the button was clicked. 单击按钮时。 I encountered the same problem you are having with cross compatibility issues. 我遇到了与交叉兼容性问题相同的问题。

To answer you question about 回答你的问题

If it's not possible, is there any other way using PHP etc? 如果不可能,有没有其他方式使用PHP等?

My opinion is you should not invoke a method on the server to do a client operation. 我的意见是你不应该在服务器上调用一个方法来进行客户端操作。 This is unnecessary overhead on your app and in my opinion, poor design/implementation. 这对您的应用程序来说是不必要的开销,在我看来,设计/实现不佳。

Now to answer your main question: 现在回答你的主要问题:

Is there a way to make it work on all of the systems/browsers (cross-browser/platform)? 有没有办法让它适用于所有系统/浏览器(跨浏览器/平台)?

To solve the issue I found a client cookie library produced by Mozilla ( https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie ) from another StackOverflow post (my apologies to the author - I don't have the link to your post). 为了解决这个问题,我在另一篇StackOverflow帖子中找到了Mozilla( https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie )制作的客户端cookie库(我向作者道歉 - 我没有链接到你的帖子)。

Using the library I create a cookie with a key of 'back-url' when the user navigates to the part of my app where I want them to be able to go back: 使用该库我创建了一个带有'back-url'键的cookie,当用户导航到我希望他们能够返回的应用程序部分时:

$('#id-of-button-clicked').click(function() {
    docCookies.setItem("back-url", window.location.href, ".myDomain.com", "/");
});

This code sets a cookie with a key-value pair 'back-url', and the current url and makes it accessible from the root directory of myDomain.com. 此代码使用键值对'back-url'和当前url设置cookie,并使其可以从myDomain.com的根目录访问。

Next, on the page where I want the user to be able to navigate back to the URL set in the cookie, I call the following code: 接下来,在我希望用户能够导航回cookie中设置的URL的页面上,我调用以下代码:

$(‘#id-of-back-button’).click(function() {
    window.location.href = docCookies.getItem('back-url');
});

This code sets the window location by getting the value of 'back-url'. 此代码通过获取'back-url'的值来设置窗口位置。

Disclaimer: I am no professional js developer - just trying to put in my two cents from some lessons I have learned. 免责声明:我不是专业的开发人员 - 只是试着从我学到的一些课程中加入我的两分钱。

Challenges to this answer: 对这个答案的挑战:

  • This answer uses cookies, many people don't like the use of cookies. 这个答案使用cookies,很多人不喜欢使用cookies。 My customers requirements allow me to use cookies. 我的客户要求允许我使用cookie。
  • I'm not encrypting the cookie - some may consider this bad practice. 我不加密cookie - 有些人可能会认为这是一种不好的做法。 I am still in the early implementation phase of our development. 我仍处于发展的早期实施阶段。 I am, however, restricting access to the cookie to only within our domain. 但是,我限制只在我们的域内访问cookie。

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

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