Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use JQuery or onbeforeunload for IE and FF

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.

like image 563
Brian Avatar asked May 21 '13 18:05

Brian


Video Answer


2 Answers

Try:

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

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

like image 108
Joe Avatar answered Oct 02 '22 03:10

Joe


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;
});
like image 31
neokjo Avatar answered Oct 02 '22 04:10

neokjo