从父页面内的 2 个框架下载相同的 JS 文件
我正在处理包含两个框架的页面。每个框架调用一个页面,然后该页面在脚本标记中调用相同的 javascript 文件。看起来有时浏览器会在另一个框架调用时缓存 js 文件,从而从缓存中抓取它。但是,有时它会下载 2 个副本,每帧一个。我试图弄清楚是否值得从父页面调用一次脚本并让每个框架的页面以这种方式访问它。那么,如果其他框架会从缓存中获取js文件,那么这只是浏览器下载js文件的速度有多快的问题吗?主要浏览器对此的正常协议是什么?
感谢您的帮助!
I am working with a page that contains two frames. Each frame calls a page that then calls the same javascript file in a script tag. It appears that sometimes the browser will have cached the js file by the time the other frame makes its call, thus grabbing it from the cache. But, it appears that sometimes it downloads 2 copies, one for each frame. I'm trying to figure out if it would be worth calling the script once from the parent page and have each frame's page access it that way. So is it just a matter of how fast the browser happens to download the js file, if the other frame will grab it from the cache? What's the normal protocol for the major browsers on this?
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以让脚本查看页面上是否有任何子 iframe,如果有,则动态地将脚本块添加到子文档(具有相同的 SRC)。这样,主进程将始终首先加载,而子进程将始终使用缓存。
You can have the script look to see if it has any child iframes on the page, if it does, dynamically add a script block to the child document (with the same SRC). This way the main one will ALWAYS load first and the children will always use the cache.
我不会对此太担心。如果到第二帧需要缓存中的文件时,它将使用缓存,如果没有,它也会加载它。每个浏览器以及每个浏览器的每个版本以不同的方式处理文件缓存,因此只需忘记它,将每个框架编码为自己的页面,并使用自己的包含内容,让浏览器担心缓存它们。
I wouldn't worry too much about it. If by the time the second frame needs the file it's in the cache, then it'll use the cache, if not, it'll load it too. Each browser, and each version of each browser, handles caching of files differently, so just forget about it, code each frame as a page of its own with its own includes and let the browser worry about caching them.