0

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.

I'm trying to use window.onunload for Mobile Safari, I can show the prompt message, but cannot prevent refreshing page.

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

<script>

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

</script>
user44858
  • 135
  • 1
  • 2
  • 9

3 Answers3

0

Read this answer

Why is jQuery unload not working in chrome and safari?

You cannot have any dialogs/confiems in beforeunload.

jQuery(window).on('beforeunload', function() {
  return 'Are you sure you want to leave this page';
});
Community
  • 1
  • 1
aWebDeveloper
  • 36,687
  • 39
  • 170
  • 242
0

As of some information from this site:

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

onbeforeunload and onunload not working in safari. So I suggest you try using the pagehide event in the safari browser. Pls try and let me know

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

  • I just tried the pagehide event, I can see the prompt but cannot prevent refreshing page. I added code sample for using pagehide() in the question, please refer. – user44858 Dec 30 '14 at 09:44
-1

Try this

$(window).on('beforeunload', function() {
  return 'Your own message goes here...';
});
Hoja
  • 1,057
  • 1
  • 12
  • 32
  • As mentioned in the question, I used beforeunload(), but it doesn't work on Mobile Safari. So I need a special handler for Mobile Safari – user44858 Dec 30 '14 at 07:47