当外部广告脚本加载缓慢时,JQuery $(document).ready() 出现问题
我有一个包含多个广告区的网站。有些是 javascript 包含,其他是 iframe 包含。 javascript include 放置在我们的结束标记之前,iframe 分散在页面各处。
今天,我在没有互联网的情况下进行本地开发,并注意到 document.ready() 内部的 javascript 在所有这些外部资源加载或超时之前不会运行。这暴露了一个漏洞,因为如果广告服务器出现故障(超出我的控制),可能会导致页面无法使用。
解决此问题的最佳实践是什么?
I have a site that contains several ad zones. Some are javascript includes, others are iframe includes. The javascript includes are placed right before our closing tag, and the iframes are scattered around the page.
Today, I was developing locally with no internet and noticed that my javascript inside of document.ready() doesn't run until all of these external assets are loaded or time-out. This exposes a vulnerability, because if an ad server goes down (out of my control), that could make the page unusable.
What are the best practices to work around this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 javascript 包含,请查看像 Yepnope 这样的脚本加载器(它也内置于 Modernizr 中,您可能已经在使用它)。对于 iFrame,您可以在 DOM 就绪后通过 JS 将它们插入到页面中吗?也许使用相同大小的空 div 并使用 jQuery/JS 将其替换为所需的 iFrame?
For the javascript includes, look into a script loader like Yepnope (which is also built into Modernizr, which you may already be using). For the iFrames, could you insert them into place in your page via JS after DOM-ready? Perhaps using empty divs with the same size and replacing them with the required iFrames with jQuery/JS?