简体   繁体   English

加载JavaScript / CSS文件

[英]Loading JavaScript / CSS files

I load a page with JavaScript and CSS files included in it from multiple iframes embedded in my flex application, are these JavaScript and CSS files loaded multiple times in the browser for each iframe? 我从我的flex应用程序中嵌入的多个iframe中加载了包含JavaScript和CSS文件的页面,这些JavaScript和CSS文件是否在浏览器中为每个iframe多次加载? If yes how can I avoid it or what are the best practices? 如果是,我该如何避免它或最佳做法是什么?

Thanks 谢谢

So here's a test on JSBin: http://jsbin.com/olome5/edit 所以这是对JSBin的测试: http//jsbin.com/olome5/edit

I loaded in the home page of Google 3 times, using different iframes. 我使用不同的iframe在Google的主页上加载了3次。

As you can see from the image below, the JS resources were loaded 3 times, even though they were the exact same files -- not loaded from cache. 从下图中可以看出,JS资源被加载了3次,即使它们是完全相同的文件 - 没有从缓存中加载。 This means that resources brought in through your iframes, at least if they're brought in simultaneously, are all loaded. 这意味着通过iframe引入的资源,至少如果它们同时被引入,都会被加载。

控制台的截图

How you could avoid this scenario, I'm not sure. 你怎么能避免这种情况,我不确定。 Instead of using iframes, you could use AJAX to load the content right into the DOM of your container page, that way, the JS / CSS would only need to be loaded into the container page and it would apply to all elements loaded within. 您可以使用AJAX将内容直接加载到容器页面的DOM中,而不是使用iframe,这样,JS / CSS只需要加载到容器页面中,它将应用于加载的所有元素。

Of course, this is tricky for JS, because all of the content brought in would then have to have the events re-attached after they were brought in, etc. This can be made easier with something like jQuery, but still isn't a small amount of effort. 当然,对于JS来说这很棘手,因为所引入的所有内容都必须在它们被引入后重新附加事件等等。这可以通过jQuery之类的东西变得更容易,但仍然不是少量的努力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM