84001

onbeforeunload and onunload getting one to work after the other

Question:

I am having some trouble with the on before unload and the unload method. I thought i set the code right in terms of the onload method only firing up after the confirm method for onbeforeunload was set a true. but sadly that isn't the case, what is happening is the unload method is starting even if the onbeforeunload method is set to false and the person wants to stay at the website. Here is the code I am working on it has changed a lot since I started hope its okay. I am sure it isn't since its not working the way I want it to.

var validNavigation = false; function wireUpEvents() { var leave_message = 'Leaving the page ?'; jQuery(function goodbye() { jQuery(window).bind('onbeforeunload', function() { setTimeout(function() { setTimeout(function() { jQuery(document.body).css('background-color', 'red'); }, 10000); },1); return leave_message; }); }); function leave() { if(!validNavigation) { killSession(); } } //set event handlers for the onbeforeunload and onunloan events window.onbeforeunload = goodbye; window.onunload=leave; } // Wire up the events as soon as the DOM tree is ready jQuery(document).ready(function () { wireUpEvents(); });

Answer1:

onbeforeunload does not work like you think it does. You can return a string from the handler, which will prompt a messsage, or undefined which will do nothing.

window.onbeforeunload = goodbye; function goodbye(e) { if (!validNavigation) { return leave_message; } else { return undefined; } }

Related: <a href="https://stackoverflow.com/questions/4650692/way-to-know-if-user-clicked-cancel-on-a-javascript-onbeforeunload-dialog" rel="nofollow">Way to know if user clicked Cancel on a Javascript onbeforeunload Dialog?</a>

Recommend

  • Go lang multiplexing all goroutines are asleep - deadlock
  • Interleaving tables and plots in R Markdown, within loop
  • How to prompt user that edits have been made upon changing pages or sorting in Kendo Grid
  • jQuery: click function bind in for-loop with closure fix
  • How to distribute an event to all nodes in a (Wildfly) cluster?
  • Mockery and Laravel constructor injection
  • Create a multiple screen android application
  • Javascript unload page condition
  • CKeditor stripping font tags instead of converting to span
  • How to access recipient on sent messages page with mailboxer
  • Regex for Specific Tag
  • in batch how do i use taskkill properly
  • Replace last two characters in column
  • IE6 changes DOCTYPE to a bad one
  • Issue with routerLink directive
  • How can we prepend rows to a react native list-view?
  • How can I replace the server in Web Component Tester
  • Clear activity stack before launching another activity
  • Looking for good analogy/examples for monitor verses semaphore
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • What's the purpose of QString?
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • Jackson Parser: ignore deserializing for type mismatch
  • Rails Find when some params will be blank
  • Record samples being played with OpenAL
  • Android full screen on only one activity?
  • Refering to the class itself from within a class mehod in Objective C
  • Typescript - Unable to get 'import' statement to function
  • formatting the colorbar ticklabels with SymLogNorm normalization in matplotlib
  • Spring Data JPA custom method causing PropertyReferenceException
  • Is there any way to access browser form field suggestions from JavaScript?
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Excel - Autoshape get it's name from cell (value)
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Weird JavaScript statement, what does it mean?
  • Matrix multiplication with MKL
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Hits per day in Google Big Query
  • File not found error Google Drive API
  • Converting MP3 duration time