下面的代码用于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">
打开新窗口/标签的a时,我该如何做?
要显示一条消息,是否应该像这样编写onbeforeunload函数
$(window).on('beforeunload', function(){
// Was the <a> tag clicked?
if(isClicked)
{
// Reset isClicked
isClicked = false;
// Display the message
alert 'You are leaving the site.';
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句