有没有办法阻止脚本加载两次? JavaScript的 [英] Is there a way to stop scripts from loading twice? JavaScript

查看:69
本文介绍了有没有办法阻止脚本加载两次? JavaScript的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个页面的应用程序,它会加载许多外部脚本。

I have an application with multiple pages which loads dozens of external scripts.

某些页面包含其他页面,这意味着有几个脚本是多次加载。

Some pages include other pages, which means several scripts are loaded multiple times.

有什么方法可以冷检查哪些脚本正在加载,然后再次阻止它们被加载?并提高性能。

Is there any way I cold check which scripts are being loaded and then stop them from being loaded a second time? And improve performance.

对不起,我没有代码示例,我刚刚开始解决这个问题。

Sorry I have no code examples, I am just beginning to tackle this issue.

有人会有任何建议吗?

推荐答案

是的,您的脚本可以创建对象,如果对象已经存在,您可以阻止重新加载

Yes, your scripts can create objects and you can prevent reload if the object exists already

参见我可以使用javascript来防止反复加载相同的脚本吗?以获取更多信息

解决问题的另一种方法是使用模块加载程序,如 require.js 来管理加载您的Javascript文件。您必须将Javascript文件转换为与require兼容的模块,但模块格式被广泛使用。由于这是一种标准而非家庭酿造的方法,它可能比我之前的建议更好。

Another way round the problem is to use a module loader such as require.js to manage loading your Javascript files. You will have to convert your Javascript files into "modules" which are compatible with require, but the module format is widely used. As this is a standard and not a home-brewed approach it is possibly a better idea than my previous suggestion.

这篇关于有没有办法阻止脚本加载两次? JavaScript的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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