Results 1 to 3 of 3

Thread: onbeforeunload

  1. #1
    Join Date
    Apr 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default onbeforeunload

    In the below code, the function closeWin() is being called all of a sudden in the middle of the page navigation. This is not happening always on one page.

    <html>
    <head>
    <script language="JavaScript">
    function closeWin() {
    var size = document.body.offsetWidth-32;
    if (event.clientY < 0 && event.clientX >= size) {
    event.returnValue = false;
    event.returnValue = 'Do you want to leave this page?';
    return false;
    }
    }
    </script>
    </head>
    <body onbeforeunload="return closeWin();">
    .....
    </body>
    </html>

    Is there any problem in the above code?

    Any suggestions are helpful.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    onBeforeUnload is IE4+ only, and so
    a) I have no experience of it, and
    b) you shouldn't use it.
    As with many things IE, whilst web-trawling for information on it, I have noticed that it appears to be particularly bug-infested.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    onBeforeUnload is IE4+ only
    That's not quite true (unfortunately). Firefox also implements it, for example.

    b) you shouldn't use it.
    I'd generally agree with that.


    Quote Originally Posted by mathurneni
    function closeWin() {
    var size = document.body.offsetWidth-32;
    if (event.clientY < 0 && event.clientX >= size) {
    event.returnValue = false;
    event.returnValue = 'Do you want to leave this page?';
    return false;
    }
    }

    [...] Is there any problem in the above code?
    If you have users with browsers other than IE then that code will error out as most browsers don't implement a global event object. I do wonder what relevance pointing device co-ordinates have to do with a window close request. Finally, return the string value from the function (not a boolean), and delete the assignments to the returnValue property. You only need to do one or the other, and the latter is better in my opinion.

    As for actual fixes, well I'm still confused about the problem. Can you provide a live demonstration?

    Mike

Bookmarks

Posting Permissions

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