Use JQuery or onbeforeunload for IE and FF
Asked Answered
H

2

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.

Hf answered 21/5, 2013 at 18:39 Comment(2)
You have $(window).bind("beforeunload",funcation(event){ - notice you spelled function wrongHopple
Thanks for letting me know. Copying code from a separate computer, so it isn't the isue.Hf
R
30

Try:

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

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

Rothko answered 21/5, 2013 at 18:41 Comment(5)
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.Hf
I just updated my code (the previous one worked everywhere but Chrome). It now works in every browser I've tested.Rothko
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?Hf
Have you included query? If not, place this: <script src="http//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> above your other code's <script> tag.Rothko
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.Hf
A
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;
});
Aquacade answered 13/11, 2014 at 16:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.