javascript 从 CDN 采购 jQuery?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8070959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sourcing jQuery from a CDN?
提问by wilsonpage
I am using require JS and want to know the best method to use a CDN version of jQuery. I hear the 1.7 version is "AMD" which is supposed to help but can't find a straight example. Hope some RequireJS gurus can help me out.
我正在使用 require JS 并想知道使用 CDN 版本的 jQuery 的最佳方法。我听说 1.7 版本是“AMD”,它应该有帮助,但找不到直接的例子。希望一些 RequireJS 大师可以帮助我。
回答by jrburke
jQuery 1.7 registers itself as an AMD module by the name of 'jquery', so you need to create a mapping for 'jquery' using the paths config:
jQuery 1.7 以“jquery”的名称将自己注册为 AMD 模块,因此您需要使用路径配置为“jquery”创建一个映射:
requirejs.config({
paths: {
'jquery' : 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min'
}
});
require(['jquery'], function($) {
//$ points to jQuery
});
Note however that RequireJS loads modules asynchronously and out of order, so if you have jQuery plugins you want to use that are not wrapped in define(['jquery'], function ($){ /* plugin code goes here */ });
calls, the plugin could execute before jQuery is loaded.
但是请注意,RequireJS 以异步和乱序方式加载模块,因此如果您要使用未包含在define(['jquery'], function ($){ /* plugin code goes here */ });
调用中的 jQuery 插件,则该插件可以在加载 jQuery 之前执行。
See the require-jqueryproject's README on ways to deal with files that depend on jQuery but do not wrap themselves in define()
calls.
请参阅require-jquery项目的自述文件,了解处理依赖于 jQuery 但不将自身包装在define()
调用中的文件的方法。
回答by murrayju
@jrburke's answer does not work for me. According to the RequireJS api doc, you should not include the file extension in the path. So here is the working code:
@jrburke 的回答对我不起作用。根据RequireJS api doc,您不应在路径中包含文件扩展名。所以这是工作代码:
requirejs.config({
paths: {
'jquery': 'https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min'
}
});
require(['jquery'], function($) {
//$ points to jQuery
});
I have a working example on jsfiddle: http://jsfiddle.net/murrayju/FdKTn/
我有一个关于 jsfiddle 的工作示例:http: //jsfiddle.net/murrayju/FdKTn/
回答by timDunham
You can include it as a dependency for a module but it's a little flakey. e.g
您可以将它包含为模块的依赖项,但它有点古怪。例如
define([
"order!http://code.jquery.com/jquery-1.7.min.js"
], function($) {
})
It's not so good for 2 reasons
它不是那么好有两个原因
1) the jquery file itself isn't a module so the $
you get from the function won't be jquery
1) jquery 文件本身不是一个模块,所以$
你从函数中得到的不会是 jquery
2) the order!
plugin doesn't work well with CDN versions of scripts. See Requirejs' order does not work with priority config and CDN dependencies
2) 该order!
插件不适用于 CDN 版本的脚本。请参阅Requirejs 的顺序不适用于优先级配置和 CDN 依赖项
I haven't had the chance to use this in a 'real' project yet because we haven't upgraded yet, but from my tests i've found that the best way is to include jquery in a script tag, then it works great as a dependency to your modules. Hopefully the following small sample will be helpful:
我还没有机会在“真正的”项目中使用它,因为我们还没有升级,但是从我的测试中我发现最好的方法是在脚本标签中包含 jquery,然后它就很好用了作为对模块的依赖。希望以下小样本会有所帮助:
<html>
<head>
<title>Index2</title>
<script src="../../scripts/libraries/jquery.js" type="text/javascript"></script>
<script src="../../scripts/libraries/require.js" type="text/javascript"></script>
<script type="text/javascript"">
require(
{baseUrl: 'scripts'},
['jquery'],
function (dollarSign) {
console.log(dollarSign('div').html('hi'));
});
</script>
</head>
<body>
<div>
</div>
</body>
</html>
回答by Alexander Matrosov
First of all, it's recommend use Protocol-relative URLfor jQuery CDN. And second, use array in value with CDN and local places for load local file if CND is dead. You can use as many CDNs urls you want. Do not afraid, it dosn't want load second file from local place if successfully download it from CDN.
首先,对于 jQuery CDN ,推荐使用Protocol-relative URL。其次,如果 CND 已死,则使用 CDN 中的数组和本地位置加载本地文件。您可以使用任意数量的 CDN 网址。不要害怕,如果从 CDN 成功下载它,它不想从本地加载第二个文件。
requirejs.config({
paths: {
'jquery': ['//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min', 'lib/jquery-1.10.2.min']
}
});