JavaScript可以告诉通过后退按钮或链接离开之间的区别吗? [英] Can JavaScript tell the difference between leaving through the back button or a link?

查看:100
本文介绍了JavaScript可以告诉通过后退按钮或链接离开之间的区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序包含多个标签页面,只需切换可见内容即可。但是,该页面还包含将向页面添加选项卡的链接。此外,应用程序会记住(使用cookie)您上次查看哪个选项卡以防刷新页面(即使使用后退和前进按钮,严格的缓存设置也会导致刷新)。

My application has pages with several tabs that simply switch the visible content. However, the page also has links that will add tabs to the page. In addition, the application remembers (with cookies) which tab you last viewed in case the page is refreshed (strict cache settings cause refreshes even when using the back and forward buttons).

我的问题是,当您第一次访问这组页面时,它应该显示第一个选项卡(选项卡A)。然后,单击一个链接,它会添加一个选项卡,并记住该新选项卡(选项卡B)。但是,如果你回击,现在看起来它什么也没做,因为它记得并显示你上次点击的标签(标签B)。

My problem is that the first time you visit this set of pages, it should show the first tab (Tab A). Then, you click a link, and it adds a tab, and it remembers that new tab (Tab B). However, if you hit back, now it looks like it did nothing because it remembers and displays the tab you last clicked (Tab B).

记住标签B是理想的行为如果您单击转发到新页面,然后使用我们的应用程序内历史记录返回上一页。但是,如果你点击后退按钮是不可取的,因为你希望它再次显示标签A,就像你第一次到达时那样。

Remembering Tab B is desirable behavior if you click forward to a new page and then use our in-application history to return to the previous page. However, it is undesirable if you click the Back Button, because you want it to again show Tab A, the way it did when you first arrived.

我的问题是JavaScript onunload 事件可以检测使用后退按钮或其他方式离开页面之间的区别。在这一点上,我希望它忘记它记住该页面的任何标签。

My question is whether the JavaScript onunload event can detect the difference between leaving the page with the Back Button, or some other means. At this point, I want it to forget any tabs that it had remembered for that page.

推荐答案

如果你正在尝试的差异检测是在用户点击链接和以其他方式离开页面之间进行检测,您可以通过将 onclick 事件处理程序附加到您想要的每个链接来使用JavaScript检测链接点击观察点击次数。

If the difference you are trying to detect is between a user clicking a link and navigating away from the page some other way, you can detect a link click using JavaScript by attaching onclick event handlers to each link you want to observe clicks on.

因此,如果 onunload 在没有 onclick 首先被解雇,用户离开页面的方式不是点击其中一条跟踪链接。

So if onunload fires without an onclick first having fired, the user is leaving the page some other way than by clicking one of your tracked links.

<script type="text/javascript">
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
    links[i].onclick = setGlobal;
}
function setGlobal() {
    window.linkClicked = true;
}
window.onunload = function() {
    if (typeof window.linkClicked === 'undefined' || !window.linkClicked) {
        // they are leaving some other way than by clicking a link
    }
}
</script>

如果你已经 onclick < a> 标记上的事件,如果是这种情况,我也可以提供一种方法。

It gets a bit trickier if you already have onclick events on your <a> tags, if that's the case I can provide a way to do that too.

当然,您也可以使用jQuery或其他JavaScript库附加到所有 onclick 事件。

Of course you can also attach to all the onclick events using jQuery or another JavaScript library.

这篇关于JavaScript可以告诉通过后退按钮或链接离开之间的区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆