javascript中采用jQuery ajax动态加载js文件的解决方法

来源:互联网 发布:oracle java tutorial 编辑:程序博客网 时间:2024/06/15 06:58
var Env = new function () {    this.envPath = null;    this.getPath = function () {        this.envPath = document.location.pathname;        this.envPath = this.envPath.substring(0, this.envPath.lastIndexOf("/") + 1);        var _scripts = document.getElementsByTagName("script");        var _envPath = null;        var _scriptSrc = null;        var self_fileName = "FormObject.js";        for (var i = 0; i < _scripts.length; i++) {            _scriptSrc = _scripts[i].getAttribute("src");            if (_scriptSrc && _scriptSrc.indexOf(self_fileName) != -1) {                break;            }        }        if (_scriptSrc != null) {            if (_scriptSrc.charAt(0) == '/') {                this.envPath = _scriptSrc.substr(0, _scriptSrc.length - self_fileName.length);            }            else {                this.envPath = this.envPath + _scriptSrc.substr(0, _scriptSrc.length - self_fileName.length);            }        }    }    this.getPath();    //导入函数    this.import = function (scriptPath) {        var _url = this.envPath + scriptPath;        if (_url.indexOf("?") != -1) {            _url += "&requestTime=" + (new Date()).getTime();        }        else {            _url += "?requestTime=" + (new Date()).getTime();        }        //alert(_url);                $.ajax({            url: _url,            dataType: "script",            async: false,  //同步            cache: false        }).done(function () {            ;        });    };    this.importCache = function (scriptPath) {        var _url = this.envPath + scriptPath;        if (_url.indexOf("?") != -1) {            _url += "&requestTime=" + (new Date()).getTime();        }        else {            _url += "?requestTime=" + (new Date()).getTime();        }        $.ajax({            url: _url,            dataType: "script",            async: false,  //同步            cache: true        }).done(function () {            ;        });    };    this.ajax = function (t_url, t_data, fn_success) {        if (t_data) {            //输入json数据不为空            $.ajax({                headers: { 'Content-Type': 'application/json' },                type: "get",                url: t_url,                cache: false,                data: t_data,                dataType: "json",                success: fn_success,                error: function (msg) {                    if (msg) {                        ShowAlert(msg.responseText);                    }                }            });        }        else {            $.ajax({                headers: { 'Content-Type': 'application/json' },                type: "get",                url: t_url,                cache: false,                dataType: "json",                success: fn_success,                error: function (msg) {                    if (msg) {                        ShowAlert(msg.responseText);                    }                }            });        }    };    //    return this;};//----导入子库 startEnv.import("jsControl.js");

0 0
原创粉丝点击