IE6中的JavaScript执行顺序 [英] Javascript Execution Order in IE6

查看:113
本文介绍了IE6中的JavaScript执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不是应该按照它们在页面上的顺序执行Javascript块吗?

Aren't Javascript blocks supposed to execute in the order that they are placed on the page?

在某些情况下,IE6似乎没有遵循该规则.我的页面基本上是这样的:

IE6 doesn't seem to follow that rule in some situations. My page basically looks like this:

<html>
<head>
    <title>Widget Sandbox</title>
    <script type="text/javascript" src="http://domain2.com/script.js"></script>
</head>
<body>
    <script type="text/javascript">
        myObj.doSomething();
    </script>
</body>
</html>

此页面托管在一个域中,而顶部的脚本托管在另一个域中. myObj对象在外部脚本中定义,并在本地脚本块中使用.有时,<body>中的脚本块在<head>中引用的脚本之前执行.

This page is hosted on one domain, while the script at the top is hosted on a second domain. The myObj object is defined in the external script and used in the local script block. Every once in a while, the script block in the <body> is executing before the script referenced in the <head>.

该问题很难重现.如果我复制上面的页面并添加一个指向重复项的链接,通常情况会更好,反之亦然,然后单击链接直到出现错误.

The issue is difficult to reproduce. I usually have better luck if I duplicate the above page and add a link to the duplicate, and vice-versa, and click the links until I get an error.

错误将是"myObj undefined",后跟行号.

The error would be "myObj undefined" followed by the line number.

有人知道我可能做错了什么吗?这仅在IE6中发生.否则,该脚本可以在IE6中完美运行95%.

Anyone have an idea of what I could be doing wrong? This only happens in IE6. The script works perfectly in IE6 95% of the time otherwise.

推荐答案

我曾经遇到过与您描述的问题非常相似的问题.

i once had a problem very similar to the one you describe.

仅在特定版本的IE6中才发生. JS正在通过Web服务器压缩,并且IE 6在JS之前(下载后但未压缩之前)执行了我的脚本...非常奇怪....

It happened only with a specific version of IE6. The JS was being gzipped by the web server and IE 6 executed my script before the JS, after it's been downloaded but before it's been unzipped... very strange....

当我在ap6中为IE6请求禁用gzip时,它解决了. (不知道您是否可以这样做) 另一种方法是设置一个计时器,以检查是否已定义MyObj....

it solved when i disabled gzip in my apache for IE6 requests. (don't know if you can do that) The alternative is to set a timer to check if MyObj is defined....

这篇关于IE6中的JavaScript执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆