简体   繁体   English

页面中的条件 onbeforeunload 事件

[英]Conditional onbeforeunload event in a page

window.onbeforeunload = function(evt) {
    var message = 'Are you sure you want to leave the page. All data will be lost!';

    if (typeof evt === 'undefined') {
        evt = window.event;
    }
    if (evt && !($("#a_exit").click)) {
        evt.returnValue = message;
    }
    return message;
};

I want user to leave the page clicking to the link (has id ="a_exit" ) only.我希望用户只点击链接(有id ="a_exit" )离开页面。 In other circumstances such as refreshing the page, clicking another link, user will be prompted that if he/she wants to leave the page.在其他情况下,如刷新页面,点击其他链接,会提示用户是否要离开页面。 I have tried to use the code above.我曾尝试使用上面的代码。 It still asks me if I want to go away when I click the exit link.当我单击退出链接时,它仍然询问我是否要离开。

$(window).bind('beforeunload',function() {
    return "'Are you sure you want to leave the page. All data will be lost!";
});

$('#a_exit').live('click',function() {
    $(window).unbind('beforeunload');
});

Above works for me以上对我有用

It will always prompt you if you want to leave the page.它会一直提示您是否要离开该页面。 It's a security issue and cannot be worked-around .这是一个安全问题,无法解决
Moreover, anything you return in the onbeforeunload event handler that is not void will be treated as the message for the prompt.此外,您在onbeforeunload事件处理程序中返回的任何非void都将被视为提示消息。 Refer to this article: https://developer.mozilla.org/en-US/docs/Web/API/Window.onbeforeunload参考这篇文章: https : //developer.mozilla.org/en-US/docs/Web/API/Window.onbeforeunload

My quick example of the conditional prompt before leaving page:我离开页面前的条件提示的快速示例:

    window.onbeforeunload = confirmExit;
    function confirmExit(event) {
        var messageText = tinymce.get('mMessageBody').getContent();
        messageText = messageText.trim();

        // ... whatever you want

        if (messageText != "")
            return true;
        else
            return void (0);
   };

It works under Chrome, FF.它在 Chrome、FF 下工作。

This worked for me.这对我有用。

   window.onbeforeunload = function(){
    if(someBoolean) {
    return 'message'
    }else{
    window.onbeforeunload = null;
    }
    }

您可以在点击处理程序中删除window.onbeforeunload

You can return null when you do not want the prompt to show.当您不想显示提示时,您可以返回null Tested on Chrome 79.在 Chrome 79 上测试。

window.onbeforeunload = () =>  {
    if(shouldPrompt){
       return true
    }else{
       return null
    }
}

BTW modern browser no longer support custom onBeforeUnload promp text.顺便说一句,现代浏览器不再支持自定义 onBeforeUnload 提示文本。

How about a variable to decide, if the link was clicked?如果链接被点击,如何决定一个变量?

var exitClicked = false;

$("#a_exit").click(function() {
  exitClicked = true;
});

window.onbeforeunload = function(evt) {
  //...

  if(evt && !exitClicked) {
    evt.returnValue = message;
  }  

  //...
};

You only set exitClicked = true when the link was really clicked and afterwards, before unloading you can simply check this variable.你只在链接被真正点击时设置exitClicked = true ,之后在卸载之前你可以简单地检查这个变量。

This is quite old but I wanted just to change the code given that .live is no longer working in Jquery.这已经很老了,但我只想更改代码,因为 .live 不再在 Jquery 中工作。 So, the updated version would be:所以,更新的版本将是:

$(window).bind('beforeunload',function() {
     return "'Are you sure you want to leave the page. All data will be lost!";
 });

 $('#a_exit').on("click", function(){
    $(window).unbind('beforeunload');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果用户拒绝在onbeforeunload事件中留下页面,那么事后是否会触发事件? - If user refuses to leave a page in the onbeforeunload event, is there an event fired afterwards? 如何取消对javascript函数调用和页面刷新的onbeforeunload事件的绑定 - how to unbind onbeforeunload event on javascript function call and page refresh 当用户未在页面主体内单击时,onbeforeunload事件不起作用 - onbeforeunload event is not working when user not click inside the body of page 使用 onbeforeunload 事件,选择停留在此页面时更改 url - using onbeforeunload event, url change on selecting stay on this page 用于记录离开页面的事件处理程序 - onunload或onbeforeunload? - Which event handler to use to record leaving page - onunload or onbeforeunload? 在IE中,对于不卸载页面的链接会触发onb​​eforeunload事件 - In IE the onbeforeunload event is fired for links that don't unload the page 如果我们不点击页面,window.onbeforeunload事件不会被触发 - window.onbeforeunload event not fired if we do not click on the page 移动设备上的Javascript onbeforeunload事件 - Javascript onbeforeunload event on mobile javascript onbeforeunload然后onunload事件 - javascript onbeforeunload then onunload event Vaadin onbeforeunload 事件 - Vaadin onbeforeunload event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM