Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Definitive answer to cross-browser window.onunload event

    Hi

    Am I correct in saying that since Opera does not reliably implement the window.onunload event, that it is NOT possible to create a predictable cross-browser trap that executes a script on window close?

    If there is a way to ensure trapping of the onunload event across all browsers, then please let me know.

    Thanks.

    Nuwanda
    Last edited by nuwanda; 06-26-2006 at 11:48 AM. Reason: errors

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    try onbeforeunload. I don't know about Opera, but it works in IE and in the latest Mozilla's versions.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Opera implements onunload. Don't think it implements non-standard events like onbeforeunload though.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Opera does not implement onbeforeunload.

    And, I've found it's trapping of onunload to be inconsistent.

    In FF, IE and Opera this script causes an alert when the window LOADS.

    NOTHING happens when the window is closed.

    Reverse behavior, it seems.

    =============================

    <script type="text/javascript">

    function cleanup(){
    alert("Leaving page...");
    }

    window.onunload=cleanup();

    </script>

    =============================

    But this (note the ommision of brackets after cleanup), causes an alert only on window close (as it should) in FF and IE but NOT in Opera.

    =============================

    <script type="text/javascript">

    function cleanup(){
    alert("Leaving page...");
    }

    window.onunload=cleanup;

    </script>

    =============================

    Hence my problem!!

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by nuwanda
    Am I correct in saying that since Opera does not reliably implement the window.onunload event, that it is NOT possible to create a predictable cross-browser trap that executes a script on window close?
    Yes, you are correct in saying that (though not just for that reason)

    But if you opened a window yourself you're okay, because you can refer to its boolean "closed" property.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #6
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, I can refer to the opened window's closed property, but only from the opener. But, all my code executes in the new window and not the opener.


  •  

    Posting Permissions

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