日期:2014-05-16  浏览次数:20271 次

动态js跨域问题解决
JS代码如下:
var DynamicLoadScriptQueue  =
{
    Loading : false,
    TaskQueue : [],
    CallBack : function (StartTime, CallBackMethod)
    {
        CallBackMethod && CallBackMethod(new Date().valueOf() - StartTime.valueOf());
        this.Loading = false;
        this.Load();
    },
    Load : function ()
    {
        if (!this.Loading && this.TaskQueue.length)
        {
            var Head = document.getElementsByTagName("head")[0];
            if (!Head)
            {
                this.TaskQueue.length = 0;
                this.TaskQueue = null;
                throw new Error('The head does not exist in this page.');
            }
            var DLSQ = this, TaskQueue = this.TaskQueue.shift(), StartTime = new Date, Script = document.createElement('script');
            this.Loading = true;
            Script.onload = Script.onreadystatechange = function ()
            {
                if (Script && Script.readyState && Script.readyState != 'loaded' && Script.readyState != 'complete') return;
                Script.onload = Script.onreadystatechange = Script.onerror = null;
                Script.Src = '';
                Script.parentNode.removeChild(Script);
                Script = null;
                DLSQ.CallBack(StartTime, TaskQueue.CallBackMethod);
                StartTime = TaskQueue = null;
            };
            Script.charset = TaskQueue.Charset || 'gb2312';
            Script.src = TaskQueue.Src;
            Head.appendChild(Script);
        }
    },
    AddTask : function (Src, Charset, CallBackMethod)
    {
        this.TaskQueue.push({ 'Src' : Src, 'Charset' : Charset, 'CallBackMethod' : CallBackMethod });
        this.Load();
    }
}

调用示例:
1、将上述代码保存为JScript1.js,编码格式选择UTF-8。
2、将以下两段代码分别报存为JScript2.js和JScript3.js,编码格式分别为UTF-8和GB2312。
var strTest = "abcdefg";