避免多次加载相同的javascript
我想防止我的系统多次加载同一个脚本,因为不同的模块可以组合,并且我使用我不想操纵的第三方库避免多次加载相同的javascript,javascript,html,Javascript,Html,我想防止我的系统多次加载同一个脚本,因为不同的模块可以组合,并且我使用我不想操纵的第三方库 以前有人这样做过吗?怎么样?似乎就是你要找的东西。怎么样?似乎正是您所需要的。由于Require JS等库并没有解决我的问题,我制定了自己的解决方案,我将在下面发布 我的系统也由不同的模块组成。在主模块中,我有一个加载程序,用于加载所有模块(php、js和css文件)的依赖项。加载依赖项后,应用程序将触发一个事件并设置一个全局变量,以防止双重包含文件 希望能有帮助。如果你有任何疑问,请告诉我 守则: //
以前有人这样做过吗?怎么样?似乎就是你要找的东西。怎么样?似乎正是您所需要的。由于Require JS等库并没有解决我的问题,我制定了自己的解决方案,我将在下面发布 我的系统也由不同的模块组成。在主模块中,我有一个加载程序,用于加载所有模块(php、js和css文件)的依赖项。加载依赖项后,应用程序将触发一个事件并设置一个全局变量,以防止双重包含文件 希望能有帮助。如果你有任何疑问,请告诉我 守则:
//Main
var main = {
init: function(){
//Dependencies to load (php, js or css)
var deps = [
'/helpers/edit/v/edit.php',
'/helpers/edit/css/edit.css',
'/helpers/validate/js/jquery.validate.js,messages_pt_BR.js'
];
//Load initial pack
if (!window.editReady){
//Load dependencies
this.load('edit',deps);
//Bind loaded event
$('body').on('editReady',function(){
//Set editLoaded to avoid double ajax requests
window.editReady = true;
//Do whatever you need after it's loaded
});
}
},
//Load external resources
load: function(name,data_urls){
var url, ext;
var len = data_urls.length;
var i = 0;
$(data_urls).each(function(){
//Get proper file
$.get(this, function(data) {
url = this.url;
ext = url.split('.').pop();
switch(ext){
case 'php':
this.appended
$(data).appendTo('body');
break;
case 'css':
$('<link/>')
.attr({
'rel':'stylesheet',
'href':url
}).appendTo('head');
break;
}
//Check if all files are included
i += 1;
if (i == len) {
$("body").trigger(name+"Ready");
}
});
});
}
};
var modules = {
themes : {
init : function(){
//Load dependencies
var deps = [
'/helpers/plupload/js/plupload.js,plupload.html5.js,plupload.flash.js'
];
if (!window.themesReady){
//Set themesReady to avoid double ajax requests
window.themesReady = true;
//Load dependencies
main.load('themes',deps);
$('body').on('themesReady',function(){
//Do whatever you need after it's ready
});
}
}
}
}
main.init();
//Main
var main={
init:function(){
//要加载的依赖项(php、js或css)
var deps=[
“/helpers/edit/v/edit.php”,
“/helpers/edit/css/edit.css”,
“/helpers/validate/js/jquery.validate.js,消息\u pt\u BR.js”
];
//加载初始包
如果(!window.editReady){
//负载相关性
加载('edit',deps');
//绑定加载的事件
$('body')。在('editReady',function()上{
//设置editLoaded以避免双重ajax请求
window.editReady=true;
//装好后,你需要做什么就做什么
});
}
},
//加载外部资源
加载:函数(名称、数据和URL){
var-url,ext;
var len=数据长度;
var i=0;
$(数据地址)。每个(函数(){
//获取正确的文件
$.get(此函数,函数(数据){
url=this.url;
ext=url.split('.').pop();
交换机(分机){
案例“php”:
这是附加的
$(数据).appendTo('body');
打破
案例“css”:
$('')
艾特先生({
“rel”:“样式表”,
“href”:url
}).appendTo(“head”);
打破
}
//检查是否包含所有文件
i+=1;
如果(i==len){
$(“body”).trigger(name+“Ready”);
}
});
});
}
};
变量模块={
主题:{
init:function(){
//负载相关性
var deps=[
“/helpers/plupload/js/plupload.js,plupload.html5.js,plupload.flash.js”
];
如果(!window.themesReady){
//设置mesready以避免双重ajax请求
window.themesReady=true;
//负载相关性
main.load('主题',dep);
$('body')。在('themesReady',function()上{
//准备好后,你需要做什么就做什么
});
}
}
}
}
main.init();
由于像requirejs这样的库并没有解决我的问题,我制定了自己的解决方案,我将在下面发布
我的系统也由不同的模块组成。在主模块中,我有一个加载程序,用于加载所有模块(php、js和css文件)的依赖项。加载依赖项后,应用程序将触发一个事件并设置一个全局变量,以防止双重包含文件
希望能有帮助。如果你有任何疑问,请告诉我
守则:
//Main
var main = {
init: function(){
//Dependencies to load (php, js or css)
var deps = [
'/helpers/edit/v/edit.php',
'/helpers/edit/css/edit.css',
'/helpers/validate/js/jquery.validate.js,messages_pt_BR.js'
];
//Load initial pack
if (!window.editReady){
//Load dependencies
this.load('edit',deps);
//Bind loaded event
$('body').on('editReady',function(){
//Set editLoaded to avoid double ajax requests
window.editReady = true;
//Do whatever you need after it's loaded
});
}
},
//Load external resources
load: function(name,data_urls){
var url, ext;
var len = data_urls.length;
var i = 0;
$(data_urls).each(function(){
//Get proper file
$.get(this, function(data) {
url = this.url;
ext = url.split('.').pop();
switch(ext){
case 'php':
this.appended
$(data).appendTo('body');
break;
case 'css':
$('<link/>')
.attr({
'rel':'stylesheet',
'href':url
}).appendTo('head');
break;
}
//Check if all files are included
i += 1;
if (i == len) {
$("body").trigger(name+"Ready");
}
});
});
}
};
var modules = {
themes : {
init : function(){
//Load dependencies
var deps = [
'/helpers/plupload/js/plupload.js,plupload.html5.js,plupload.flash.js'
];
if (!window.themesReady){
//Set themesReady to avoid double ajax requests
window.themesReady = true;
//Load dependencies
main.load('themes',deps);
$('body').on('themesReady',function(){
//Do whatever you need after it's ready
});
}
}
}
}
main.init();
//Main
var main={
init:function(){
//要加载的依赖项(php、js或css)
var deps=[
“/helpers/edit/v/edit.php”,
“/helpers/edit/css/edit.css”,
“/helpers/validate/js/jquery.validate.js,消息\u pt\u BR.js”
];
//加载初始包
如果(!window.editReady){
//负载相关性
加载('edit',deps');
//绑定加载的事件
$('body')。在('editReady',function()上{
//设置editLoaded以避免双重ajax请求
window.editReady=true;
//装好后,你需要做什么就做什么
});
}
},
//加载外部资源
加载:函数(名称、数据和URL){
var-url,ext;
var len=数据长度;
var i=0;
$(数据地址)。每个(函数(){
//获取正确的文件
$.get(此函数,函数(数据){
url=this.url;
ext=url.split('.').pop();
交换机(分机){
案例“php”:
这是附加的
$(数据).appendTo('body');
打破
案例“css”:
$('')
艾特先生({
“rel”:“样式表”,
“href”:url
}).appendTo(“head”);
打破
}
//检查是否包含所有文件
i+=1;
如果(i==len){
$(“body”).trigger(name+“Ready”);
}
});
});
}
};
变量模块={
主题:{
init:function(){
//负载相关性
var deps=[
“/helpers/plupload/js/plupload.js,plupload.html5.js,plupload.flash.js”
];
如果(!window.themesReady){
//设置mesready以避免双重ajax请求
window.themesReady=true;
//负载相关性
main.load('主题',dep);
$('body')。在('themesReady',function()上{
//准备好后,你需要做什么就做什么
});
}
}
}
}