Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8577249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:07:01+00:00 2026-06-11T20:07:01+00:00

I am having some trouble with the on before unload and the unload method.

  • 0

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();
 });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:07:02+00:00Added an answer on June 11, 2026 at 8:07 pm

    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: Way to know if user clicked Cancel on a Javascript onbeforeunload Dialog?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some trouble creating a before aspect for a method that has
I've used jScrollPane before but I'm having some trouble getting it right this time.
I'm having some trouble with getting input from the command line before opening a
I'm having some trouble implementing DoubleBuffer into my program. Before you faint from the
I am having some trouble updating UMDF drivers using devcon during a standard code-deploy-debug
I'm having some trouble getting this code to compile on Linux but it works
I am having some trouble with compiling my code due to these error messages
I'm having some trouble with this code. I am trying to write a function
I'm having some trouble with displaying numbers in apex, but only when i fill
I'm having some trouble fading in large images, but only in Chrome. Here's the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.