喜悦国际村 专业PHP开发者社区's Archiver

游戏人间 发表于 2010-3-12 11:01 AM

IE6下动态载入JS脚本解决方案

[i=s] 本帖最后由 游戏人间 于 2010-3-12 01:52 PM 编辑 [/i]

function GetJsonLoad()
    {
        var script      = document.createElement("script");
        script.src      = globallUrl;
        script.id       = "script";
        script.type     = "text/javascript";
        if (document.all)
        {
            script.onreadystatechange = function()
            {
                if (this.readyState == "complete" || this.readyState == "loaded")
                {
                    if (typeof xx != 'undefined')
                    {
                        // 这里就可以正常的使用JS中的变量了
                        // 只可惜IE下好像并不能马上使用
                        // 而如果用setInterval让这个函数执行两次的话第二次就能OK了
                        // 谁有使用经验,因为是跨子域了并且需要登录,所以AJAX回调数据的方案或用代理都有点小困难
                    }
                }
            }
        }
        // 这里已经把其它可以用onload的浏览器部分代码删除了
        document.body.appendChild(script);
    }

sanders_yao 发表于 2010-3-12 11:22 AM

尝试一下jquery的getScript

游戏人间 发表于 2010-3-12 11:35 AM

[i=s] 本帖最后由 游戏人间 于 2010-3-12 11:36 AM 编辑 [/i]

我们用的prototype,我都在怀疑是prototype影响的

后来有人说是“当该js数据是no-cache, no-store,并且是gzip压缩的时候就会出现这个问题”  [url]http://www.javaeye.com/topic/543572[/url]

郁闷了...

zw870307 发表于 2010-3-12 01:06 PM

这个加载编辑器倒挺有用

fly512 发表于 2010-3-12 01:41 PM

让加载的那个JS执行当前页面的函数不就可以了

游戏人间 发表于 2010-3-12 01:51 PM

[quote]让加载的那个JS执行当前页面的函数不就可以了
[size=2][color=#999999]fly512 发表于 2010-3-12 13:41[/color] [url=http://www.phpx.com/redirect.php?goto=findpost&pid=1146328&ptid=259291][img]http://www.phpx.com/images/common/back.gif[/img][/url][/size][/quote]

一样的不行

未注册会员 发表于 2010-3-12 03:39 PM

[quote]

一样的不行
[size=2][color=#999999]游戏人间 发表于 2010-3-12 01:51 PM[/color] [url=http://www.phpx.com/redirect.php?goto=findpost&pid=1146330&ptid=259291][img]http://www.phpx.com/images/common/back.gif[/img][/url][/size][/quote]
不要用document.body.appendChild(script); 
试下:
var head = document.getElementsByTagName("head")[0] || document.documentElement;
                head.insertBefore( script, head.firstChild );

fly512 发表于 2010-3-12 05:40 PM

怎么可能呢?看演示
[url]http://www.gaomaozi.com/test/request.html[/url]
向 [url]http://64.71.167.22/test/server.php[/url] 发请求

返回的header 信息
[code]
Server        nginx/0.7.61
Date        Fri, 12 Mar 2010 09:38:17 GMT
Content-Type        text/html
Transfer-Encoding        chunked
Connection        keep-alive
X-Powered-By        PHP/5.1.6
Cache-Control        no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Content-Encoding        gzip
[/code]

fly512 发表于 2010-3-12 05:42 PM

我的也是IE6

游戏人间 发表于 2010-3-12 06:10 PM

[i=s] 本帖最后由 游戏人间 于 2010-3-12 06:14 PM 编辑 [/i]

你这是页面载入的时候就执行的。

我那是页面载入完成后,由客户端的用户点击某些东西之后去动态载入一段JS,这段JS是由另一子域下生成的json。
而且还是跨子域的,我不知道你直接用IP的方式是不是被JS认为已经是同一个域下。至少我测试的结果是跨子域的情况下AJAX是不行的(IE6)。

这是有差别的.

fly512 发表于 2010-3-12 06:28 PM

:dizzy: 我用的不是AJAX,我那是跨域,就是创建一个script 去请求

未注册会员 发表于 2010-3-12 08:36 PM

[i=s] 本帖最后由 未注册会员 于 2010-3-12 08:38 PM 编辑 [/i]

装了IE8,没有测试环境。Jquery源码里:
// [color=#ff0000]Use insertBefore instead of appendChild  to circumvent an IE6 bug.[/color]
// This arises when a base node is used (#2709 and #4378).
head.insertBefore( script, head.firstChild );

页: [1]

Powered by Discuz! Archiver 7.0.0  © 2001-2009 Comsenz Inc.