【问题标题】:Fire document.ready on back button IE11在返回按钮 IE11 上触发 document.ready
【发布时间】:2014-06-03 19:12:27
【问题描述】:

我有一个带有搜索表单的页面。当您第一次访问该页面时,我有一个 jQuery(document).ready() 触发:

jQuery(document).ready(
  function () {
    jQuery("#new_search").val(Math.round(new Date().getTime() / 1000));
  }
);

现在,当您提交表单然后点击返回按钮时,document.ready 不会触发,它需要触发。

最初的问题只是在 Firefox 中,但经过一些研究我发现在 body 中添加一个 unload 事件解决了这个问题:

jQuery(window).unload(function(){});

现在,我被告知它在 IE11 中不起作用。

任何人有任何提示可以在 IE11 中触发 document.ready 函数吗?

【问题讨论】:

  • 你在这里使用history.pushState吗?
  • 不,我不知道那是什么

标签: javascript jquery internet-explorer-11


【解决方案1】:

所以,我在搜索中偶然发现了this answer,埋在帖子的底部。

我试了一下,效果很好:

// break the bfcache (ie11 and all others likely)
jQuery(window).focus(
  function() {
    jQuery("#new_search").val(Math.round(new Date().getTime() / 1000));
  }
);

我原封不动地保留了原始代码,因为在我的情况下,我只是设置了一个 unix 时间戳,如果它发生多次也没关系。

完整答案:

jQuery(document).ready(
  function () {
    jQuery("#new_search").val(Math.round(new Date().getTime() / 1000));
  }
);

jQuery(window).unload(function(){});

jQuery(window).focus(
  function() {
    jQuery("#new_search").val(Math.round(new Date().getTime() / 1000));
  }
);

【讨论】:

    【解决方案2】:

    对于遇到此问题的其他任何人,这里有一篇 MSDN 文章记录了如何阻止 IE11 的后向/前向缓存行为:https://msdn.microsoft.com/library/dn265017(v=vs.85).aspx。据此,添加 beforeunload 事件处理程序会停止它。这也适用于 Firefox:https://developer.mozilla.org/en-US/docs/Using_Firefox_1.5_caching

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-17
      • 1970-01-01
      • 2013-09-27
      • 1970-01-01
      • 2014-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多