1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

IE7 completely ignores window.onbeforeunload

Discussion in 'JavaScript' started by seismicmike, Jan 20, 2009.

  1. #1
    I'm using the jQuery framework, but I don't think that should affect anything. I can't get IE7 to respond to window.onbeforeunload. It was working for a while, but now it completely ignores it.

    I'm building a site with a series of forms that the user needs to fill out. I'm using window.onbeforeunload to capture the user leaving the page before submitting it and saving his progress using Ajax calls to send the data to the database. Works like a charm in Firefox and Chrome, but I can't even get IE7 to respond to:

    
    window.onbeforeunload = function() {
         alert('This is a test of the window.onbeforeunload function. 
                Had this been an actual window.onbeforeunload function, you 
                would have been provided with further instructions.');
    }
    
    Code (markup):
    I load the page, hit refresh, try leaving to another page, anything to get this to pop up, and I get nothing in IE7.

    Any suggestions? Could this be a security setting - seeing as how Microsoft considers window.onbeforeunload to be a security weakness that malicious sites can exploit? If it is a security thing, then perhaps I need to find another way to do what I'm trying to do? I've already given the users a button to manually save their progress, but I'd still like to be able to force it if they leave, at least to update their progress - or to alert them that any progress made since the last time the form was saved would be lost.. you know... out of courtesy.

    Thanks in advance.
     
    seismicmike, Jan 20, 2009 IP
  2. seismicmike

    seismicmike Peon

    Messages:
    63
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here's slightly more information about how this is supposed to work:

    
    Step                    Works in FF          Works in GC          Works in IE             Works in FF IETab
    1. Give user Y/N           Y                        Y                        N                             N
        box b/4 leaving
    
    2. Save Data                Y                         Y                       N                             Y
    
    
    3. Inform user that         Y                         N                       N                             N
        data has been
        saved.
    
    Code (markup):
    Thanks again.
     
    seismicmike, Jan 20, 2009 IP
  3. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #3
    Hi mike,

    if you're using the code posted, it shouldn't work on FF neither. Javascript won't allow to have unterminated strings on multiple lines. So simply you need to rewrite your code :
    alert('This is a test of the window.onbeforeunload function. '
    +'Had this been an actual window.onbeforeunload function, you '
    +'would have been provided with further instructions.');
     
    lp1051, Jan 20, 2009 IP
  4. seismicmike

    seismicmike Peon

    Messages:
    63
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I apologize for the lack of clarity. That's not the problem. That's not how it is in my code. I only did that here for readability sake. Sorry for the confusion.

    Thanks for taking the time to try to help me, though :)

    In retrospect it would probably have been clearer to say:

    
    window.onbeforeunload = function() {
         alert("Hello World.");
    }
    
    Code (markup):
     
    seismicmike, Jan 20, 2009 IP
  5. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #5
    Thanks. That's pity, because I don't know about other problem there. IE7 should not have problems with registering this event. The only thing might be when trying to remove the event.
    But then I made test for you. When I set IE security level on HIGH onbeforeunload is not registered. But this behavior is repro on IE6 too, so I'm not sure what's exactly the issue in your case. Maybe somebody else knows...
     
    lp1051, Jan 20, 2009 IP
  6. seismicmike

    seismicmike Peon

    Messages:
    63
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    'sok. thanks for trying. I've got my security settings as low as possible for IE7. I might also mention that I'm using IE7 standalone because I still want to have IE6 on my computer. Would that change anything?

    Thanks.

    PS. For those of you just tuning in...... I've also tried the following jQuery syntax, with the same result:

    
    $(window).bind('beforeunload', function(e) {
         alert("Hello World.");
    }
    
    Code (markup):
    I've found the advantage of using the non-jQuery syntax in this case, returning a string will cause the site to give a confirmation box with the string as the text. IE:

    
    window.onbeforeunload = function() {
         return "Are you sure?";
    }
    
    Code (markup):
    will prompt the user with an "Are you sure?" message. (In FF and Chrome that is... It should do in in IE7 as well, but it's not... hence my thread here... :)) Yes (or OK), will allow the user to continue to leave, and No (or Cancel), will cause the user to stay on the page. Whereas, jQuery isn't the same:

    
    $(window).bind('beforeunload', function(e) {
        return "Are you sure?";
    }
    
    Code (markup):
    That doesn't seem to work in any browser.
     
    seismicmike, Jan 20, 2009 IP
  7. gnp

    gnp Peon

    Messages:
    137
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #7
    try assigning a predifined function (and not define it on the fly), as this was a bug on IE 6 (early versions..)

    Perhaps it has resurfaced on this version as well.. ( i do not have IE7 to test...)

    
    function verifyexit()
    {
    	return 'Are you sure ?';
    }
    window.onbeforeunload = verifyexit;
    
    Code (markup):
    hth ..
     
    gnp, Jan 20, 2009 IP
  8. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #8
    what, this won't work with an anonymous function? possible memory leak aside - do you have any link/info on when/how such an assignment will fail in IE6? not that i am running head over heels to support IE6 as it's 8 years old now... but still - something like 20% of visitors in the uk are using it for whatever idiotic reason and that just cannot be ignored...

    onbeforeunload and onunload can be handy for IE6 and IE7 in running some sort of a garbage collection that unbinds events and other possible leaks... it's an area i am beginning to explore now but I will definitely need to be aware of such bugs...

    cheers.
     
    dimitar christoff, Jan 22, 2009 IP
  9. gnp

    gnp Peon

    Messages:
    137
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #9
    :eek: I have apparently flopped here ..

    I read the code i found completely wrong and mixed it with another bug which existed in ie6 sp1..

    the links
    http://codingforums.com/showthread.php?t=115610 (the suggestion to use predefined function, although was about non IE browsers..)
    and
    http://support.microsoft.com/kb/331869 (talking about an IE bug with onbeforeunload not triggering if the body is empty ..)

    sorry for the confusion..

    (walks to corner)
     
    gnp, Jan 22, 2009 IP