为什么 Javascript 有时无法在 IE8 中加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9366918/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:25:45  来源:igfitidea点击:

Why does Javascript sometimes not load in IE8

javascriptinternet-explorer-8

提问by damaniel

I have external javascript files that get loaded in my master page.

我在我的母版页中加载了外部 javascript 文件。

Sometimes in IE8 my javascript files do not get loaded up correctly and the browser throws a bunch of errors saying object not recognized.

有时在 IE8 中,我的 javascript 文件没有正确加载,浏览器会抛出一堆错误,说对象无法识别。

If I refresh the page then everything is ok. If I click on a link then the problem sometimes occurs again.

如果我刷新页面,则一切正常。如果我点击一个链接,那么问题有时会再次出现。

I do have meta tags in my header for clearing out the cache on each request.

我的标题中有元标记,用于清除每个请求的缓存。

Also I am using head.load to load my js files in parallel.

此外,我使用 head.load 并行加载我的 js 文件。

The head.load script is in my header and then external files are at the end of my body.

head.load 脚本在我的标题中,然后外部文件在我的正文末尾。

Please remember that this problem only occurs in IE8. So my question is..is there a hack that I can use to make sure my javascript files are loaded correctly each time the page loads for IE8?

请记住,此问题仅在 IE8 中出现。所以我的问题是..是否有一个技巧可以用来确保每次为 IE8 加载页面时我的 javascript 文件都正确加载?

Any help would be greatly appreciated.

任何帮助将不胜感激。

Updatedas requested

按要求更新

<head runat="server">
    <meta http-equiv="CACHE-CONTROL" content="NO-CACHE" />
    <meta http-equiv="PRAGMA" content="NO-CACHE" />
    <meta http-equiv="EXPIRES" content="-1" />

    <script type="text/javascript" src="scripts/js/head.load.min.js"></script>
</head>
<body>
<script type="text/javascript">
    head.js("scripts/js/jquery-1.6.2.min.js");
         head.js("scripts/js/jquery.cookie.js");
    head.js("lib/gritter/jquery.gritter.min.js");
    head.js("lib/fancybox/jquery.easing-1.3.pack.js");
    head.js("lib/fancybox/jquery.fancybox-1.3.4.pack.js");
    head.js("scripts/js/jquery.microaccordion.js");
    head.js("scripts/js/jquery.stickyPanel.js");
    head.js("scripts/js/guidely.js");
    head.js("scripts/js/pto.js");
</script>
</body>

采纳答案by damaniel

Ok the problems appears to be how the head.load file loads my externals in IE8. If I load jquery before I load my head.load file and then load my externals in parallel at the end of the page, then the their are no errors on the page.

好的,问题似乎是 head.load 文件如何在 IE8 中加载我的外部组件。如果我在加载 head.load 文件之前加载 jquery,然后在页面末尾并行加载我的外部,那么它们在页面上就没有错误。

This is definitely a solution but I wish that even jquery can be loaded into the head without javascript errors in IE. I'm sure this won't be an error in latter versions.

这绝对是一个解决方案,但我希望即使是 jquery 也可以加载到头部而不会在 IE 中出现 javascript 错误。我相信这在后面的版本中不会出错。

Thank-you everyone for the input.

谢谢大家的意见。

@ frederic - I work at a company that has 188,000 employees. If they have an add on installed in their browser that is causing my page to crash then there is nothing I can do about that. I also don't think that this problem is being caused by any add ons. I think its just how head.load is handled in IE8.

@ frederic - 我在一家拥有 188,000 名员工的公司工作。如果他们在浏览器中安装了导致我的页面崩溃的附加组件,那么我对此无能为力。我也不认为这个问题是由任何附加组件引起的。我认为它就是在 IE8 中处理 head.load 的方式。

@Shard - I don't think you understand what I was asking and if thats the case then I'm sorry for not being more clear. Anyway I found a solution so thank-you for your input.

@Shard - 我认为你不明白我在问什么,如果是这样,那么我很抱歉没有说得更清楚。无论如何,我找到了一个解决方案,非常感谢您的投入。

<head runat="server">
    <meta http-equiv="CACHE-CONTROL" content="NO-CACHE" />
    <meta http-equiv="PRAGMA" content="NO-CACHE" />
    <meta http-equiv="EXPIRES" content="-1" />
    <script type="text/javascript" src="scripts/js/jquery.min.js"></script>
    <script type="text/javascript" src="scripts/js/head.load.min.js"></script>
</head>

<script type="text/javascript">
    head.js("scripts/js/jquery.cookie.js");
    head.js("lib/gritter/jquery.gritter.min.js");
    head.js("lib/fancybox/jquery.easing-1.3.pack.js");
    head.js("lib/fancybox/jquery.fancybox-1.3.4.pack.js");
    head.js("scripts/js/jquery.microaccordion.js");
    head.js("scripts/js/jquery.stickyPanel.js");
    head.js("scripts/js/guidely.js");
    head.js("scripts/js/pto.js");
</script>

回答by Jorge E. Cardona

Look at the page http://headjs.com/in the section Usage you will see exactly what is wrong with your code:

查看页面http://headjs.com/的用法部分,您将确切地看到您的代码有什么问题:

// files are loaded in parallel and executed in order they arrive head.js("file1.js"); head.js("file2.js"); head.js("file3.js");

// 文件并行加载并按到达顺序执行 head.js("file1.js"); head.js("file2.js"); head.js("file3.js");

Your scripts are been loading in parallel and are executed in arrival time, that's pretty bad for the deps on jquery, if you want them to be executed in order use this:

您的脚本正在并行加载并在到达时间执行,这对 jquery 上的依赖项非常不利,如果您希望它们按顺序执行,请使用以下命令:

head.js("scripts/js/jquery-1.6.2.min.js",
        "scripts/js/jquery.cookie.js",
        "lib/gritter/jquery.gritter.min.js"),
        "lib/fancybox/jquery.easing-1.3.pack.js",
        "lib/fancybox/jquery.fancybox-1.3.4.pack.js",
        "scripts/js/jquery.microaccordion.js",
        "scripts/js/jquery.stickyPanel.js",
        "scripts/js/guidely.js",
        "scripts/js/pto.js");

You need to call js with a list of resources to load, not just call js several times.

您需要使用要加载的资源列表调用 js,而不仅仅是多次调用 js。