【发布时间】:2015-03-10 01:04:12
【问题描述】:
我想要完成的伪代码:
if(in dev environment){
//write unminified scripts to the head tag
}
else{ // in production
// write the minified distribution script to the head
}
选项 1。) 在 Chrome 桌面浏览器中间歇性工作,在 Safari Mobile 中间歇性工作。
function write_to_head (path ,js_file_src){
var js_script = document.createElement('script');
js_script.type = "text/javascript";
js_script.src = path + js_file_src;
document.getElementsByTagName('head')[0].appendChild(js_script);
}
选项 2。) 在 Chrome 中 100% 工作,在 Safari Mobile 中间歇性工作。在 async:false 时似乎也能更好地工作,但这是不行的,因为它可能会阻止用户交互。
function load_script(path ,js_file_src){
$.ajax({
async:true, // false may block user interaction
type:'GET',
url:path +""+ js_file_src,
data:null,
cache: true,
success:_onsuccess,
error: _onerror,
dataType:'script'
});
}
我根据我是在开发环境还是生产环境中调用以下函数之一。
var dev_scripts = ["script1.js","script2.js","script3.js",........etc];
var prod_scripts = ["all-dist.min.js",........etc];
function load_dev(){
for(var i=0;i<dev_scripts.length;i++){
// call write_to_head ("/api/path/",dev_scripts [i] );
// or
// load_script("/api/path/",dev_scripts [i] );
}
}
function load_prod(){
for(var i=0;i<prod_scripts .length;i++){
// call write_to_head ("/api/path/",prod_scripts [i] );
// or
// load_script("/api/path/",prod_scripts [i] );
}
}
有没有更好的方法可以在所有移动和桌面浏览器上完成我上面讨论的内容?
【问题讨论】:
标签: javascript jquery performance development-environment production-environment