如何重新加载javascript文件 [英] how to reload javascript file

查看:123
本文介绍了如何重新加载javascript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于重新加载JavaScript文件的问题。我开发网页前端并在JS中进行大量编码。每当我想检查应用程序的流程是否正确(或进行一些调试)时,我必须按 F5 键重新加载整个页面及其脚本,我必须等待。等待时间取决于页面的重量,有时我会不耐烦。因此,我想知道是否有任何方法只重新加载更改的脚本文件 *。js ?或者可能有一些Chrome重新加载所选脚本的插件?这可能是一种非常方便的发展方式。感谢您的回复。

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,例如:

this one uses jQuery, it is here for example:

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();


推荐答案

据我所知,没有办法要做到这一点。问题是,一旦评估完成,你就无法从页面中卸载一个脚本。

As far as I know, there is no way to to do that. The problem is, you just cannot unload a script from a page, once it was evaluated.

即使您删除了< script> 节点,它也不会改变行为。例如,有可能使用某些第三方插件,但我几乎可以肯定它不可能与任何vanilla js实现。

Even if you remove the <script> node, it will not change the behavior. It might be possible with some third-party plugin for instance, but I'm almost sure its not possible with any vanilla js implementation.

当然你可以加载来自服务器的相同脚本文件并执行(eval)它,但同样,你仍然没有卸载以前的代码,这可能导致非常不可预见的行为。

Of course you could just load the same script file from your server and execute (eval) it, but again, you still didn't unload the previous code which could lead to very unexpectable behavior.

所以,你必须把你的F5钥匙放在脚趾上。

So, you have to keep your F5 key on its toes.

这篇关于如何重新加载javascript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆