I have a question about reloading JavaScript files. I develop web pages front-ends and do a lot of coding in JS. Every time when I want to check an application's flow is correct (or do some debugging) I have to hit F5 key to reload whole page and it's scripts, and I have to wait. Waiting time depends on page's weight and sometimes I get impatient. Therefore I wonder if there is any way to reload only changed script file *.js
? or maybe there is some plugin for Chrome to reload chosen script? It could be a very handy way to ease development. Thank you for replies.
这个使用 jQuery,例如:
var scs=$('script[type*=javascript]');
var scsl = scs.length;
var scsi = 0;
var o = [];
var oi = 0;
var ol = 0;
var s = '';
var fws = 0;
var sws = 0;
var v = {};
function i1(){
fws = window.setInterval(function(){
v = $(scs[scsi]);
s = v.attr('src');
if(typeof s!='undefined' && s.indexOf('http')==-1 && s.indexOf('index')==-1){
console.log([scsl,scsi,s]);
o.push({src:s});
v.remove();
}
if(scsi==scsl){
console.log(o);
window.clearInterval(fws);
ol = o.length;
i2();
}
else{
scsi++;
}
},800);
}
function i2(){
sws=window.setInterval(function(){
v = o[oi];
sc = $('<script>').attr({type:'text/javascript',src:v.src+'?t='+(new Date().getTime())});
console.log([ol,oi,v.src]);
$('head').append(sc);
if(oi==ol-1){
window.clearInterval(sws);
}
else{
oi++;
}
},800);
}
i1();
据我所知,没有办法做到这一点。问题是,你不能unload页面中的脚本,一旦被评估。
即使您删除了<script>
节点,它不会改变行为。例如,使用某些第三方插件可能是可能的,但我几乎可以肯定任何普通的 js 实现都不可能。
当然,您可以从服务器加载相同的脚本文件并执行(评估)它,但同样,您仍然没有卸载以前的代码,这可能会导致非常意外的行为。
因此,您必须时刻保持 F5 键的状态。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)