简体   繁体   English

无法阻止刷新window.onunload()中的页面

[英]Cannot prevent refreshing page in window.onunload()

I'm trying to show a prompt message in case user trying to refresh the page. 我正在尝试显示提示消息,以防用户尝试刷新页面。 I used window.onbeforeunload and it's working fine on other browsers BUT not Mobile Safari. 我使用window.onbeforeunload,它在其他浏览器(而不是Mobile Safari)上可以正常工作。

I'm trying to use window.onunload for Mobile Safari, I can show the prompt message, but cannot prevent refreshing page. 我正在尝试将window.onunload用于Mobile Safari,我可以显示提示消息,但不能阻止刷新页面。

Below is the code 下面是代码

<script>
    var iOS = ( navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ? true : false );
    if(iOS){

        window.onunload = function() {
            var test = confirm("Are you sure you want to leave this page");

            if(!test){
                return false;
            } 

        }
    }

I also tried the pagehide event, can show the prompt but still cannot prevent refreshing page Below is the code 我也尝试了pagehide事件,可以显示提示,但仍然无法阻止刷新页面,下面是代码

<script>

  window.addEventListener("pagehide", function(){
    var test = confirm("Are u sure you want to leave this page aaaaaaaaaaaaa");         
    if(!test){          
        return false;
    } 
  },false);

</script>

Read this answer 阅读此答案

Why is jQuery unload not working in chrome and safari? 为什么jQuery卸载无法在chrome和safari中使用?

You cannot have any dialogs/confiems in beforeunload. 卸载前不能有任何对话框/信任。

jQuery(window).on('beforeunload', function() {
  return 'Are you sure you want to leave this page';
});

As of some information from this site: 截至本网站的一些信息:

https://www.webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/ https://www.webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/

onbeforeunload and onunload not working in safari. onbeforeunload和onunload在Safari中不起作用。 So I suggest you try using the pagehide event in the safari browser. 因此,我建议您尝试在Safari浏览器中使用pagehide事件。 Pls try and let me know 请尝试让我知道

http://www.w3schools.com/jquerymobile/event_pagehide.asp http://www.w3schools.com/jquerymobile/event_pagehide.asp

Try this 尝试这个

$(window).on('beforeunload', function() {
  return 'Your own message goes here...';
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM