0

Is there a way to ensure a javascript file loads just once when reloading an HTML file (which references this javascript file)?

e.g.:

<script src="js/myscript.js" type="text/javascript"></script>

I would like this command to be something like:

<script src="js/myscript.js" type="text/javascript" opt="include_once"></script>

something like the PHP command include_once()...

... Is there a native way to do this (through the native script tag)? Or is it possible only with third-party libraries, like jQuery?

Thank you!

4

1 回答 1

1

有没有一种本地方法可以做到这一点(通过本地脚本标签)?

不会。浏览器通常会处理缓存,不会两次下载脚本。但他们会执行两次。因此,您要么必须更改脚本以在遇到自身预先存在的实例时保释,要么您可以使用脚本加载器并设置一些值以不再加载它。

这是一个流行的命名空间方法,在 js/myscript.js 中:

var customApp = customApp || {
    // do your stuff here and if customApp already existed it will just skip this part
};
于 2013-06-05T19:18:23.797 回答