www.webdeveloper.com
Results 1 to 4 of 4

Thread: Prevent leaving a page with JavaScript

  1. #1
    Join Date
    Jun 2007
    Posts
    1

    Prevent leaving a page with JavaScript

    I'm in the process of creating a web page that allows for remote control of a microcontroller, and I'm looking to build in a feature that requires a user to confirm that they would like to leave the page if they try to navigate away from the control site. I don't want to use the confirm() method, but would rather like to have a popup window appear with two buttons ("Leave the site", "Return to site") that control the action of the main window.

    As of right now, I've been able to create the popup window but am stuck on trying to control the navigation of the main window. Is there any method that would allow me to pause the pending navigation to another page in order for the popup to execute first and then have the main window respond? Another alternative would be to somehow grab the address the user wants to view, store that in a variable, and then access that variable in the popup...I tried that as well to no avail.

    Additionally, it seems as though simply closing either the tab or the main window (in IE and Firefox) completely bypasses my popup command. Is there any way to prevent that from happening?

    Here is the code that I'm using for my main page:
    Code:
    <html>
    <body>
    
    XXXXXXX //irrelevant custom code for control
    
    <script language="javascript">
    function confirmation()
    {
    
    navigate("/failsafe.html", "exit", "width=800px, height=600px")
    
    }
    
    window.onunload=confirmation;
    </script>
    
    </body>
    </html>
    And here's my popup code:
    Code:
    <html>
    <body>
    
    <font size="6"><center>Warning!</center></font><br><br>
    
    <center>
    <form>
    <input type="button" value="Leave the Control Webpage" onclick="window.close()" />
    </form>
    <form>
    <input type="button" value="Return to the Control Webpage" onclick="window.navigate('/saavtest.html')" />
    </form>
    </center>
    
    </body>
    </html>
    I know there's a way to name windows and access some of their variables, but as a noob to JavaScript I don't know the extent of that control. Any help would be much appreciated.

    --Q

  2. #2
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,773
    How about something like this:
    Code:
    window.onclose = function() {
      showConfirmWindow();
      return false;
    };
    
    
    function showConfirmWindow() {
      // Show the window and text. Clicking the OK button executes a function
      // called leavePage();
    }
    
    
    function leavePage() {
      window.close();
    }

  3. #3
    Join Date
    Sep 2008
    Posts
    1

    onbeforeunload

    Use window.onbeforeunload instead of window.onunload. That way you can cancel leaving the page. Only use onunload if you are certain that you will be leaving the page.

  4. #4
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,974
    onbeforeunload is non standard so only exists in some browsers.

    The event handler itself or JavaScript in its entirety can be turned off so and then any message will not appear.
    Stephen

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles