我在JS片段下面对浏览器执行某些操作,或者关闭/调用其他应用程序。
<script>
$( document ).ready(function() {
$(".btn").click(function () {
localStorage.setItem("isReadyCalled","false");
});
$(":submit").click(function () {
localStorage.setItem("isReadyCalled","false");
});
$(":button").click(function () {
localStorage.setItem("isReadyCalled","false");
});
$("a").click(function () {
localStorage.setItem("isReadyCalled","false");
});
$("form").submit(function () {
localStorage.setItem("isReadyCalled","false");
});
$(":input").click(function () {
localStorage.setItem("isReadyCalled","false");
});
});
localStorage.setItem("isReadyCalled","false");
window.onbeforeunload = function(){
if(localStorage.getItem("isReadyCalled") == "true" ){
//Do some action
}
};
localStorage.setItem("isReadyCalled","true");
</script>
这在Chrome和Mozilla中运行良好。 在IE中它适用于大多数地方。但有时它会在准备好之前调用onbeforeunload,这导致即使没有设置标志也会执行onbefore中的代码。
不确定为什么会发生这种情况。 任何帮助表示赞赏。
答案 0 :(得分:0)
这不起作用?
<script>
var is_ready = false;
$( document ).ready(function() {
is_ready = true
...
});
localStorage.setItem("isReadyCalled","false");
window.onbeforeunload = function(){
if(!is_ready)
return
if(localStorage.getItem("isReadyCalled") == "true" ){
//Do some action
}
};
localStorage.setItem("isReadyCalled","true");
</script>