在 CDN 被阻止/不可用的情况下,如何加载本地脚本文件作为后备?
我正在为以下 javascript 使用 CDN:
- https://ajax.googleapis .com/ajax/libs/jquery/1.5.1/jquery.min.js
- https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js
- http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.min.js
- http://ajax.aspnetcdn.com/ajax/jquery.validate/1.7/jquery。 validate.min.js
对于每一个,如何在可能被阻止/不可用的实例中恢复使用本地副本?
I’m using a CDN for the following javascript:
- https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
- https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js
- http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.min.js
- http://ajax.aspnetcdn.com/ajax/jquery.validate/1.7/jquery.validate.min.js
For each one, how can I revert to using local copy in the instance where it may be blocked/unavailable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
要确认 cdn 脚本已加载,您可以检查此脚本定义的任何变量/函数是否存在,如果未定义 - 则 cdn 失败,您需要加载本地脚本副本。
基于此原则的解决方案如下:(
如果没有定义的 window.jQuery 属性,则未加载 cdn 脚本)。
您可以使用此方法构建自己的解决方案。例如,jquery 工具提示插件创建
$.tooltip()
函数,因此我们可以使用如下代码检查它:To confirm that cdn script loaded you can check for existence any variable/function this script defines, if it is undefined - then cdn failed and you need to load local script copy.
On this principle are based solutions like that:
(if there is no window.jQuery property defined cdn script didn't loaded).
You may build your own solutions using this method. For instance, jquery tooltip plugin creates
$.tooltip()
function so we can check it with code like this:我会研究一个像 yepnopejs 这样的插件,
需要一个对象数组来检查,请检查 网站
I would have looked into a plugin like yepnopejs
Takes an array of object to check for, check the documentation at the site
取自 HTML5 样板。
Taken from HTML5 Boilerplate.
我使用 http://fallback.io/
I use http://fallback.io/
第一件事 - 你不应该以不同的顺序包含它们吗?
像这样的事情应该有效:
我在这里所做的只是检查第一个插件(jQ validate)是否已加载。通过检查 jQuery.fn 对象上的静态
validate
函数。我无法以同样的方式检查第二个脚本,因为它没有在任何地方添加任何内容,只是代理现有方法,因此更容易假设如果第一个脚本有效,第二个脚本也将有效 - 毕竟,它们是由相同的 CDN。first thing - shouldn't you include them in different order?
something like this should work:
what I'm doing here is simply checking if the first plugin (jQ validate) has been loaded. by checking for a static
validate
function on jQuery.fn object. I can't check the second script same way, because it's not adding anything anywhere, just proxying existing methods, so it's easier to assume that if the first one works, the second one will work too - after all, they are provided by the same CDN.您需要知道如何确保库已成功加载。例如:
所以这尝试从 google CDN 加载 jQuery (1.5.1)。由于
标签确实会阻止整体渲染和渲染。执行过程(如果没有明确告知不同),我们可以立即检查是否在
window
中找到jQuery
对象。如果没有,只需将另一个标记写入文档并引用本地副本即可进行回退。
You need to know, how you can make sure that a lib was loaded successfully. For instance:
So this trys to load jQuery (1.5.1) from the google CDN. Since
<script>
tags do block the overall render & execution process (if not explicitly told different), we can check right after that if thejQuery
object is found withinwindow
. If not, just fallback by writing another<script>
tag into the document, referencing a local copy.以下解决方案通过了 HTML5、XHTML 1.0 Transitional 和其他 HTML 风格的验证。将以下内容放在每个外部 JQuery 调用之后。请务必将 jquery.min.js 替换为 JQuery 脚本本地副本的路径。
如果您不使用 unescape,则在使用 http://validator.w3.org 进行验证时会出现错误因为属性规范列表中不允许使用“%”。
HTML5 Boilerplate 示例在与旧版 HTML 一起使用时也存在验证错误:
如果您仅针对 HTML5 和未来的 HTML 风格进行开发,那么您可以使用 HTML5 Boilerplate 解决方案,但由于您可能会发现自己将部分代码插入到旧版 HTML 中,因此请使用它这种一刀切的方法是安全的。
您需要为每个外部托管脚本指定不同的函数。例如,JQuery Tooltip 插件创建 $.tooltip() 函数,因此您可以使用以下命令检查它:
The following solution passes validation for both HTML5, XHTML 1.0 Transitional and other HTML flavors. Place the following after each of your external JQuery call. Be sure to replace jquery.min.js with the path to your local copy of the JQuery script.
If you don't use unescape, you'll have errors when validating with http://validator.w3.org since "%" is not allowed in an attribute specification list.
The HTML5 Boilerplate example also has validation errors when used with older HTML:
You'll be fine with the HTML5 Boilerplate solution if you are developing only for HTML5 and future HTML flavors, but since you may find yourself inserting portions of your code into legacy HTML, play it safe with this one-size-fits-all approach.
You'll need to specify a different function for each externally hosted script. For instance, the JQuery Tooltip plugin creates the $.tooltip() function, so you can check it with the following:
我在 jquery ui - 如何使用 google CDN 回答了类似的问题
您可以使用以下方式进行调用
。您还可以通过更改主题名称来链接到其他 Ui 主题。在这种情况下,将名称 base 更改为任何其他主题名称
/base/jquery-ui.css
到任何其他主题。查看 jQuery UI 博客,了解所有 CDN 链接 http://blog.jqueryui.com/
如果您想在 Google 失败的情况下恢复到您的主机,您可以这样做
I answered a similar questions at jquery ui - how to use google CDN
You can make the call using
You can also link to other Ui themes by changes the name of the theme. In This case change the name base to any other theme name
/base/jquery-ui.css
to any other theme.Check out the jQuery UI Blog for a link of all CDN links http://blog.jqueryui.com/
If you want to revert back to your host in case Google failed, you can do
取自 这篇文章
Taken from this article
最好使用您自己的 Javascript 代码加载所有脚本。
首先尝试通过将新的 SCRIPT 元素插入 DOM 来加载 CDN 文件。然后通过查找它定义的对象来检查它是否已加载。如果该对象没有出现,则插入另一个 SCRIPT 元素来加载本地副本。也许最好清理 DOM 并删除无法加载的 SCRIPT。
不要忘记考虑计时问题,即加载不是即时的。
Best to do all this script loading with your own Javascript code.
First try to load the CDN file by inserting a new SCRIPT element into the DOM. Then check that it has loaded by looking for an object that it defines. If the object does not appear, then insert another SCRIPT element to load the local copy. Probably best to clean up the DOM and remove SCRIPTs which failed to load as well.
Don't forget to account for timing issues, i.e. load is not instant.