您的位置:首页 > Web前端 > JQuery

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

2016-04-19 19:09 766 查看
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;
};
//----导入子库 start
Env.import("jsControl.js");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: