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 5 of 5
  1. #1
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts

    emulating onbeforeunload

    Hey guys

    Does anyone know how to emulate an onbeforeunload event in browsers using the Gecko DOM - essentially I want to allow an AJAX request to be called without being cancelled when the page unloads.

    This is for an intranet application - a CMS in fact. There's a legitimate reason for doing this - it's to do with locking html pages to prevent them being edited by more than one user - the onunload "unlocks" the page. I've got it working using an alert onunload to leave enough time for the AJAX request to process, but would like a more discrete way of doing this.

    Because of the sensitive nature of the implications (if used wrongly) of a solution to this request, feel free to PM me with replies to this thread. I'm also very aware that there's an extremely high chance that this is simply not possible - I'm a pretty experienced developer and cannot think of a way to do this.

    Many thanks

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #2
    New Coder
    Join Date
    Sep 2005
    Location
    Ottawa, ON
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    no need to emulate it. It's already there.

    Try it:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    
    <html>
    <head>
    	<title>Untitled</title>
    <script>
    function onBeforeUnloadEvent() {
      return 'hello';
    }
    </script>
    
    </head>
    
    <body onbeforeunload="return onBeforeUnloadEvent();">
    
    
    
    </body>
    </html>

  • #3
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Nice one, not quite sure how i missed that
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Relying on "unload" event in application like this will result in "permanently" locked pages that you would have to manually go into database and unlock. There is a number of ways the browser can close, such as computer crash, power failure, connection failure, that would cause your application logic failure.
    Since you are already using XMLHTTP calls to server, a more robust way would be to "ping" the server every so often (say, 30 sec should be no problem as far as extra resources go) to indicate that the page is in use.
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #5
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Cheers for the response mate, in fact I set use IIS/ASP Application objects and session events and match valid session id's, so if the session ends (for whatever reason) the CMS handles this. There's no database usage for locking pages.

    Cheers for the response, still amazed that I missed onunload in Gecko browsers!!

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper


  •  

    Posting Permissions

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