要求js从CDN加载脚本失败 [英] require js loading scripts from cdn fail

查看:224
本文介绍了要求js从CDN加载脚本失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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文档:

请勿在构建中将CDN加载与shim config混合使用.示例方案:从CDN加载jQuery,但使用shim配置加载类似于jQuery的Backbone的普通版本.在进行构建时,请确保在内建文件中内联jQuery,并且不要从CDN加载它.否则,Backbone将内联到生成的文件中,并在CDN加载的jQuery加载之前执行.这是因为shim config只是延迟文件的加载,直到加载依赖项为止,但不对define进行任何自动包装.构建后,依赖关系已经内联,shim配置无法将非define()编码的执行推迟到以后. define()'模块在构建后即可使用CDN加载的代码,因为它们将其源代码正确包装在define工厂函数中,直到加载依赖项后,这些函数才会执行.因此,课程:shim config是非模块化代码,传统代码的权宜之计. define()'s模块更好.

Do not mix CDN loading with shim config in a build. Example scenario: you load jQuery from the CDN but use the shim config to load something like the stock version of Backbone that depends on jQuery. When you do the build, be sure to inline jQuery in the built file and do not load it from the CDN. Otherwise, Backbone will be inlined in the built file and it will execute before the CDN-loaded jQuery will load. This is because the shim config just delays loading of the files until dependencies are loaded, but does not do any auto-wrapping of define. After a build, the dependencies are already inlined, the shim config cannot delay execution of the non-define()'d code until later. define()'d modules do work with CDN loaded code after a build because they properly wrap their source in define factory function that will not execute until dependencies are loaded. So the lesson: shim config is a stop-gap measure for non-modular code, legacy code. define()'d modules are better.

简而言之,我相信当您拥有不是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加载脚本失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆