对 IE 和 FF 使用 JQuery 或 onbeforeunload
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16677188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use JQuery or onbeforeunload for IE and FF
提问by Brian
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)
我在 Flex4 应用程序中工作,在“index.template.html”文档中使用 javascript。我在使用 Firefox 时无法使用 onbeforeunload。该应用程序在 IE 中运行良好,但完全相同的应用程序不适用于 FF。(见下文)
<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.
根据我的发现,FF 似乎没有注册 onbeforeunload 事件,所以我发现使用的流行的东西是与 JQuery 绑定。所以,我删除了上面的代码并用下面的代码替换它,但是当用户尝试在 IE 和 FF 中离开页面时,它不会显示弹出窗口。任何似乎为此使用 JQuery 的人似乎都在做同样的事情,所以我不知道发生了什么。
<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.
最终,像在代码的第一部分那样调用“flex.unloadMethod”会很好,但目前我只是想让一个弹出窗口工作,所以我知道我在正确的轨道上。任何见解将不胜感激。
回答by Joe
Try:
尝试:
<script>
$(window).on('beforeunload', function(){
return "This should create a pop-up";
});
</script>
Example: http://jsfiddle.net/AeztA/3/
回答by neokjo
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.
想补充一点,我发现您不能在 Firefox 中使用空字符串。例如作为返回,它必须至少为 1 个空白。
var text = 'Exit Message';
$(window).on('beforeunload', function(){
return " " + text;
});