问题描述
我是RequireJS的新手,由于某种原因,我无法通过CDN加载脚本.
I am new to RequireJS, and for some reason I cannot load scripts through a CDN.
我的代码:
// site full url
var siteUrl = window.location.protocol+"//"+window.location.host + "/fresh/";
// requirejs config
requirejs.config({
baseUrl: siteUrl + "assets/js/",
paths: {
"plugins": "plugins",
"scripts": "scripts",
"jquery": "https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min",
"jquery-ui": "https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min",
"bootstrap": "https://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/js/bootstrap.min",
}
});
require(['jquery', 'jquery-ui', 'bootstrap', 'plugins/chosen'], function($, chosen){
/*
loading global selectors and variables
*/
//chosen for select boxes
$(".chzn-select").chosen();
});
并且jquery无法加载.我收到以下错误:
and jquery fails to load. I get the following errors:
ReferenceError: jQuery is not defined
[Megszakítás ennél a hibánál]
...h"):this.container.removeClass("chzn-container-single-nosearch")),e="",s=this.re...
chosen.js (1. sor)
ReferenceError: jQuery is not defined
TypeError: $(...).chosen is not a function
$(".chzn-select").chosen();
请问有人指出我做错了吗?
Could please someone point out what I am doing wrong?
P.S:我正在定义站点URL,因为我使用的是Laravel,如果没有该定义,它将在基本URL中包含URL段.
P.S: I am defining the site URL because I am using Laravel, without that definition it includes the URL segments in the base URL.
推荐答案
布兰登是正确的,而我认为补片无法解决问题
Brandon is correct, while I think shim could not solve the problem
来自 RequireJS文档:
简而言之,我相信当您拥有不是requirejs的模块并且它们依赖于某些其他模块(jQuery)时,您将无法在RequireJS中使用CDN中的jQuery.
In short, I believe when you have modules that are not requirejs and they depend on some other modules (jQuery), you won't be able to use jQuery from CDN inside RequireJS.
在这种情况下,我同意布兰登的建议,直接在页面上要求可能会更好.
In this case I agree with what Brandon suggested, requiring them directly on the page may be better.
这篇关于要求js从CDN加载脚本失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!