【发布时间】:2014-03-04 23:35:40
【问题描述】:
下面的代码使用onbeforeunload 在用户离开网站之前显示一条消息。
$(window).on('beforeunload', function(){
// Was the <a> tag clicked?
if(isClicked)
{
// Reset isClicked
isClicked = false;
// Display the message
return 'You are leaving the site.';
}
});
// Define a clicked state variable with a default value;
isClicked = false;
$(window).load(function(){
// Get all tags with the specified className
var anchors = $('.leaveSite');
// Add the "onclick" listener to each one of them
anchors.each(function(index) {
$(this).click(function(){
isClicked = true;
});
});
});
我认为 onbeforeunload 在打开新窗口/标签时起作用。
当用户点击打开一个新窗口/标签的<a class="leaveSite"> 时,我该如何做到这一点?
【问题讨论】:
标签: jquery html browser dom-events