【发布时间】:2014-04-10 12:24:32
【问题描述】:
我需要检查用户是否关闭了浏览器。没有可靠的方法,但最准确的方法似乎是使用onbeforeunload 并检查是否单击了链接或按钮。我还将添加 f5 和其他一些额外的检查。
但我的按钮有问题。如果我点击按钮,即使有if(window.link_was_clicked==false)条件也会进行ajax调用。
<button onclick="setLocation('http://dev.site.com/checkout/')" class="button" title="Checkout" type="button"><span><span>Checkout</span></span></button>
还有脚本:
jQuery(document).on('click', 'button', function(event) {
window.link_was_clicked= true;
});
window.onbeforeunload = function() {
if(window.link_was_clicked==false){
//make ajax call
}
};
问题似乎是因为按钮onclick 附加了setLocation 函数。有没有办法先触发jQuery(document).on?
【问题讨论】:
-
你说的是哪个按钮?你说的是浏览器关闭按钮吗?
-
您的 HTML 在哪里?如果你没有使用
<button>标签,那么你的 jQuery 不会选择任何东西。 -
你的 HTML 太稀疏了。
-
@Sushil 我说的是
标签: javascript jquery