如何阻止< a>的window.on('beforeUnload')事件标签? [英] How do I block the window.on('beforeUnload') event for <a> tag?

查看:527
本文介绍了如何阻止< a>的window.on('beforeUnload')事件标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,当用户想要使用 X按钮关闭窗口/标签时,我需要获得用户确认提醒。

In my project I need to get the user confirmation alert , when the user want to close the window/tab using X button.

window.on('beforeUnload')也适用于超链接。如何阻止离开页面警告< a>标签

But the window.on('beforeUnload') also works for hyper link . How can I block this leave page alert for <a> tag ?

我的 JSP 将具有

<a href="next.jsp" id="navigate"> click here </a>

我的 Jquery 将有,

$(document).ready(function(){
    $('#navigate').on('click', function() {
    stopNavigate(event);
    });

});

function stopNavigate(event){   
    $(window).off('beforeunload', function(){

    });
}


$(window).on('beforeunload', function(){
    return 'Are you sure you want to leave?';
});

$(window).on('unload', function(){
    logout();


});

我不希望离开消息提醒当用户点击任何链接时。我只需要窗口关闭操作 离开消息提醒

I don't want the leave message alert when the user click any links. I need the leave message alert only for window close operation.

推荐答案

$(document).ready(function () {
    $('#navigate').on('mouseenter', stopNavigate)
        .on('mouseout', function () {
        $(window).on('beforeunload', windowBeforeUnload);
    });

});

function stopNavigate(event) {
    $(window).off('beforeunload');
}

function windowBeforeUnload() {
    return 'Are you sure you want to leave?';
}

$(window).on('beforeunload', windowBeforeUnload);

DEMO

这篇关于如何阻止&lt; a&gt;的window.on('beforeUnload')事件标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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