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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1873290/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:42:00  来源:igfitidea点击:

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

phpjavascript

提问by user187580

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 等吗?

回答by Robert Cabri

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

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

回答by Klaus Byskov Pedersen

You should consider doing it like this instead:

你应该考虑这样做:

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

回答by Cander

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>

回答by Manikandan

The below code is working.

下面的代码正在工作。

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

回答by Pedro Picante

If anyone is still struggling with this issue try removing the href-attribute from the link you want to use window.history.back()with. I'm not sure if this workaround complies with HTML-standards but it worked out fine for me.

如果有人仍在为此问题苦苦挣扎,请尝试从要使用window.history.back()的链接中删除href属性。我不确定这个解决方法是否符合 HTML 标准,但对我来说效果很好。

回答by B... James B.

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).

为了解决我发现由Mozilla(制作客户端的cookie库的问题https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie)从另一个StackOverflow上后(我的道歉作者-我没有指向您帖子的链接)。

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.

免责声明:我不是专业的 js 开发人员 - 只是想从我学到的一些课程中投入我的两分钱。

Challenges to this answer:

这个答案的挑战:

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