SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using onbeforeunload

    I see where you can use onbeforeunload to prompt the user with a message and based on the return value of the message either continue with the unload or not.

    What I want to to is skip the prompt and just do the unload based on a situation on the form. If I do a return false; it goes ahead and displays a message box with the statement 'false'. Is there a way to cancel the unload without first asking the user a question?

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bump.

    One last try - no one can help me out here?

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,600
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    onbeforeunload is a proprietary Microsoft event only recognised by Internet Explorer. It is therefore only suitable for use on intranets where you know everyone is running IE. On the internet it is useless as 10% of people will have Javascript disabled so no event runs and 10 - 20% of visitors will be running a web browser that ignores onbeforeunload.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall
    onbeforeunload is a proprietary Microsoft event only recognised by Internet Explorer. It is therefore only suitable for use on intranets where you know everyone is running IE. On the internet it is useless as 10% of people will have Javascript disabled so no event runs and 10 - 20% of visitors will be running a web browser that ignores onbeforeunload.

    This is for a work website where the company standard is IE - that's not a problem for me.

  5. #5
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,449
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by felgall
    onbeforeunload is a proprietary Microsoft event only recognised by Internet Explorer. It is therefore only suitable for use on intranets where you know everyone is running IE. On the internet it is useless as 10% of people will have Javascript disabled so no event runs and 10 - 20% of visitors will be running a web browser that ignores onbeforeunload.
    I'm pretty sure Gecko-based browsers (Mozilla, Firefox, and Netscape 8) do support this.

  6. #6
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,449
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Girevik
    I see where you can use onbeforeunload to prompt the user with a message and based on the return value of the message either continue with the unload or not.

    What I want to to is skip the prompt and just do the unload based on a situation on the form. If I do a return false; it goes ahead and displays a message box with the statement 'false'. Is there a way to cancel the unload without first asking the user a question?
    Post your code. That will allow our JavaScript gurus to troubleshoot with you.

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right now it's pretty straightforeward just as a test.

    Code:
    window.onbeforeunload=bunload()
    
    function bunload {
    
    return false
    
    }

  8. #8
    d^_^b Iain G's Avatar
    Join Date
    Nov 2005
    Location
    Gloucester, UK
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm afraid I don't really follow what you're after, but you need to lose the parentheses after the function assignment to the handler. If you include them, the bunload function executes as soon as the code is read by the browser.
    Code:
    window.onbeforeunload = bunload;
    
    function bunload {
    
    return false;
    
    }
    Coding and design philosophy:
    Keep It Simple, Stoopid

  9. #9
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I'm after is to control the unload (either allow it or not) based on criteria on the page rather than from input from the user. Is there anyway to do that without the popup box?

    From what I've seen, I'm gathering the answer is "no".

  10. #10
    I believe you have my stapler. scrubz's Avatar
    Join Date
    Feb 2001
    Location
    Van down by the river
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've run into this before.

    From http://msdn.microsoft.com/library/de...foreunload.asp ...

    "The default statement that appears in the dialog box, 'Are you sure you want to navigate away from this page? ... Press OK to continue, or Cancel to stay on the current page.', cannot be removed or altered."

    Also of note are the number of methods that can fire onbeforeunload, which are also listed on that page. This can be a real pain when the event fires at times you don't want it to.


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
  •