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");
相关文章推荐
- jQuery Validation用法示例
- [Infopath]使用jquery给infopath表单的的field赋值。 how to set value to Infopath field by Jquery
- jQuery-JSONP 插件跨域调用功能(Uncaught SyntaxError: Unexpected token : 出错原因解释)
- 高效Web开发的10个jQuery代码片段(转)
- jquery ajax提交表单
- (转)【深入浅出jQuery】源码浅析2--奇技淫巧
- JQuery 动态添加onclick事件
- jquery每日一学
- 扩展jQuery实现日期联动
- Jquery实现的简单轮播效果【附实例】
- jQuery实现选项联动轮播效果【附实例】
- chosen.jquery 插件 动态设置下拉选项被选中selected
- JQuery 对 Select option 的操作
- jquery操作select取值,设置选中(转载)
- jQuery.merge 源码阅读
- jQuery事件处理
- jquery笔记-遍历checkbox的对应行文本内容-ajax传递checkbox
- jQuery的选择器
- Jquery pagination分页示例
- JQuery学习笔记(一)