javascript - 延迟加载 Javascript,对象不是从 IE8 缓存创建的

标签 javascript caching internet-explorer-8

不幸的是,该错误不会发生在我的应用程序之外!

场景

index.php

<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').'GMT');
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Lazy loader</title>
</head>
<body>
...
<script type="text/javascript" src="internal.js"></script>
...
</body>
</html>

内部.js

myApp = {
    timerHitIt: false,
    hitIt: function () {
        if (arguments.callee.done) { return; }
        arguments.callee.done = true;
        if (myApp.timerHitIt) { clearInterval(myApp.timerHitIt); }
        var elt = document.createElement("script");
        elt.async = true;
        elt.type = "text/javascript";
        elt.src = "external.js";
        elt.onload = elt.onreadystatechange = function () { alert(typeof(something)); };
        document.body.appendChild(elt);
    }
};
if (document.addEventListener) { document.addEventListener("DOMContentLoaded", myApp.hitIt, false); }
/*@cc_on @*/
/*@if (@_win32)
    document.write("<script id=__ie_onload defer src="+((location.protocol == "https:") ? "//:" : "javascript:void(0)")+"><\/script>");
    document.getElementById("__ie_onload").onreadystatechange = function () {
        if (this.readyState == "complete") { myApp.hitIt(); }
    };
/*@end @*/
if (/WebKit/i.test(navigator.userAgent)) {
    timerHitIt = setInterval(function () {
        if (/loaded|complete/.test(document.readyState)) { myApp.hitIt(); }
    }, 10);
}
window.onload = myApp.hitIt;

外部.js

something = {};
alert(true);

有效结果为

  • 未定义 -> true -> 对象(±新请求)
  • true -> 对象(± 缓存的 JavaScript)

但有时,当按 F5 时,我会得到

  • true -> 未定义

有人知道为什么执行了alert(true)但没有设置某些内容吗?

最佳答案

解决方案,甚至修复了 Internet Explorer 6

window.something = {};

关于javascript - 延迟加载 Javascript,对象不是从 IE8 缓存创建的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2896528/

相关文章:

javascript - WordPress在标题中自定义js

javascript - 隐藏文本框,直到选中相关复选框 - Javascript

android - ios和html5应用缓存测试

javascript - Internet Explorer (7/8) 无法循环 javascript 数组

jQuery hide() 在 IE 8 上无法按预期工作

jquery - 在 Fancybox 弹出的 IE8 中,关闭按钮未完全显示

javascript - 如果只有一个更改,为什么 angularJs 会更新数组/散列表的所有元素?

javascript - Chrome 扩展 : Get Current Console to Send Up to Server

java - Infinispan相当于ehcache的copyOnRead和copyOnWrite

session - Cakephp tmp 目录 : Cache and Session Folders