13

I'm working in a Flex4 application, using javascript, in the "index.template.html" document. I'm having an issue being able to use onbeforeunload with Firefox. The application works perfectly in IE, but the exact same one doesn't sit well with FF. (See below)

<script type="text/javascript">
window.onbeforeunload=before;
window.onunload=after;

function before(evt)
{
   var flex=document.$(application)||window.$(application);
   flex.unloadMethod(); //custom method to log out the user
}

function after(evt)
{

}
</script>

From what I've found, FF doesn't seem to register onbeforeunload events, so I found that the popular thing to use instead is binding with JQuery. So, I deleted the above code and replaced it with the below code, but it doesn't display a pop-up when the user tries leaving the page in both IE and FF. Anyone that seems to be using JQuery for this seems to be doing the exact same thing, so I don't know what's going on.

<script type="text/javascript">
$(window).bind("beforeunload",function(event){
   return "This should create a pop-up";
});
</script>

Eventually it would be nice to call the "flex.unloadMethod" like in the first bit of code, but for the time being I'm just trying to get a pop-up to work so I know I'm on the right track. Any insight would be greatly appreciated.

Brian
  • 529
  • 2
  • 7
  • 17
  • You have `$(window).bind("beforeunload",funcation(event){` - notice you spelled `function` wrong – Ian May 21 '13 at 18:51
  • Thanks for letting me know. Copying code from a separate computer, so it isn't the isue. – Brian May 21 '13 at 19:09

2 Answers2

30

Try:

<script>
    $(window).on('beforeunload', function(){
        return "This should create a pop-up";
    });
</script>

Example: http://jsfiddle.net/AeztA/3/

Joe
  • 15,205
  • 8
  • 49
  • 56
  • Thanks for the reply, but I've tested that and it didn't work. I was also told not to use an alert to create the pop-up. From my experience, it also just is an alert, so it only has an OK button to press (no cancel). Also, you are in unload which will be way to late to try to log someone out if they are exiting my page. – Brian May 21 '13 at 18:47
  • I just updated my code (the previous one worked everywhere but Chrome). It now works in every browser I've tested. – Joe May 21 '13 at 18:48
  • I'm testing your new code in IE, but when I change the URL or exit the window, the pop-up doesn't appear. Is there something I need to import? – Brian May 21 '13 at 19:01
  • Have you included query? If not, place this: `` above your other code's ` – Joe May 21 '13 at 19:02
  • 4
    Okay, now that makes a lot of sense. I work in a closed environment so I'm unable to load libraries from the web. Thanks so much for your help! I would upvote you, but I need 15 points. When I get them, I'll be sure to come back. – Brian May 21 '13 at 19:06
1

Would like to add that i figured out that you can't use an empty string in firefox. It has to be at least 1 blank for example as return.

var text = 'Exit Message';

$(window).on('beforeunload', function(){
    return " " + text;
});
SeinopSys
  • 8,787
  • 10
  • 62
  • 110
neokjo
  • 11
  • 1