javascript - Beforeunload not working properly? -


.html:

<html> <header>     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>     <script src="js/game.js"></script> </header> <body> heya! </body> </html> 

.js:

window.onbeforeunload = function() {     savedata(); }  function savedata() {     return "hello"; } 

i tried:

$(window).unload(function() {     savedata();     return true; }); 

the 1 found working put:

<body onbeforeunload="return savedata()"> 

but wouldn't show message ("hello", showed "do want leave page blabla") etc. want able save stuff before page closed, without alert, using alert try out.

the event onbeforeunload works properly.

if want make ask user if wants leave page, must return something:

window.onbeforeunload = function() {   return savedata(); } 

but if want stuff in background, before page left, can put whatever want inside event, , work properly. take @ example below (open console):

window.onbeforeunload = function() {    console.log('leaving');  }
<a href='http://www.stackoverflow.com/'>go stackoverflow</a>

note: if use prompt or alert, browsers won't allow, because they're obtrusive user.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -