【问题标题】:Internet explorer fires 'on(beforeunload)' when lightbox pops up弹出灯箱时,Internet Explorer 会触发“on(beforeunload)”
【发布时间】:2013-03-19 20:32:14
【问题描述】:

这是我的代码:

$(window).on('beforeunload', function() {
   if ($('.jqmCustomize').css('display') == 'block')
     return 'You have not save your customization yet.';
});

这在 chrome 和 firefox 上运行良好,但在 Internet Explorer 上,当我的灯箱在同一页面中打开时,它会触发 on('beforeunload') 事件。有什么办法可以防止这种情况发生吗?我试图:

var close=true;
window.onbeforeunload = closeConfirmation;
function closeConfirmation(){
   if(close)
     return 'You have not save your customization yet.';
}

但我仍然得到相同的结果。

【问题讨论】:

  • 你能详细解释一下这个问题吗?你的.jqmCustomize是什么,灯箱是什么,你想做什么?
  • .jqmCustomize 是我使用 jqmodal 插件的灯箱。我正在检查 jqmCustomize 是否可见,而不是在关闭页面之前询问用户是否要关闭,因为他们将有一些数据要保存在 jqmCustomize 上。这在 chrome 和 ff 上运行良好,但在 ie 上,当 jqmCustomize 灯箱打开时,即认为用户单击了关闭页面按钮,并触发 onbeforeunload。

标签: javascript jquery internet-explorer onbeforeunload


【解决方案1】:

实际上我没有发现任何问题。 可能是我使用的是旧版本的 jqmodal 和 jquery,但它可以按预期工作:

http://jsfiddle.net/xqhLx/5/

$(function($){
  $('#jqModal').jqm();

   $('a').live('click',function(){
      $('#jqModal').jqmShow();    
      return false;
  }); 
});

$(window).on('beforeunload', function() {
   if ($('.jqmWindow').css('display') == 'block')
     return 'You have not save your customization yet.';
});

HTML:

<a href="/" >Test</a>

<div id="jqModal" class="jqmWindow" style="display: none;">This is the test</div>

【讨论】:

  • 更新到最新版本的 jqmodal 但仍然有同样的问题。
  • 你检查我的例子了吗?是你所期望的吗?
  • 是的,你的例子是我期望在我的情况下发生的。但是ie在显示jqmodal时触发beforeunload事件。嗯,就是点击 的时候。
  • 我猜你需要return false 来防止重定向到另一个页面。
  • 这可能是由于 ajax 调用引起的吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多