使用带有 jQ​​uery“未定义”的 Javascript 加载器

发布于 2024-11-17 01:45:25 字数 624 浏览 3 评论 0原文

我正在使用 Javascript 加载器 [ requireJS ],它与内容并行加载脚本 - 但是,我有一个问题。即

require('http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js');

通常 - 作为“备份” - 我使用过

<script type="text/javascript">
  if (typeof jQuery == 'undefined') {
    document.write(unescape("%3Cscript src='/scripts/jquery-1.4.4.min.js' type='text/javascript'%3E%3C/script%3E"));
  }
</script>

但是,当使用 java 脚本加载器时 - 这将始终呈现 jQuery“未定义” - 因为 JS 和内容是并行加载的。

效果基本上是您正在加载 jQuery 2x - 即通过 javascript 加载器加载 1x,通过“jquery == undefined”加载 1x。

如何使“备份”与 javascript 加载器一起工作?

I am using a Javascript Loader [ requireJS ] which loads scripts in parallel to content - however, I have a problem. i.e.

require('http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js');

Typically - as a 'backup' - I've used

<script type="text/javascript">
  if (typeof jQuery == 'undefined') {
    document.write(unescape("%3Cscript src='/scripts/jquery-1.4.4.min.js' type='text/javascript'%3E%3C/script%3E"));
  }
</script>

However, when using a java-script loader - this will ALWAYS render jQuery "undefined" - because JS and content is loaded in parallel.

The effect is basically that you are loading jQuery 2x - i.e. 1x through your javascript loader and 1 through "jquery == undefined".

How can I make the "backup" work with a javascript loader ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给不了的爱 2024-11-24 01:45:25

据我所知,requirejs 通常是这样使用的:

require(['http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js'],
    function() {
        // Code that depends on jQuery here.
    }
);

依赖于 jQuery 的函数只有在 jQuery 加载时才会被调用。但如果 jQuery 无法加载,依赖它的代码将永远不会被执行。

由于您想在这种情况下尝试使用本地 jQuery,因此您可能应该捕获脚本加载超时错误并尝试从其他源加载 jQuery。 (但请注意,超时错误的速度很慢。)

docs 中有关错误处理的信息很少:

要检测错误,您可以重写 require.onError() 来获取错误。如果是超时问题,传递给 onerror 函数的错误对象将包含两个属性:

  • requireType:值将为“超时”
  • requireModules:超时的模块名称/URL 数组。

我认为,代码可能如下所示(未经测试):

var functionThatDependsOnJquery = function() {
    // Code that depends on jQuery here.
};

require.onError = function(err) {
    # If we have Google jQuery load timeout...
    if (err.requireType = "timeout" &&
        err.requireModules.indexOf('http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js') != -1
        ) {
        # ...we'll try to load it from another source.
        require(['/scripts/localJquery'], functionThatDependsOnJquery);
    }
    # We have another script load timeout, so we just throw an error
    # as requirejs normally does.
    else { throw err; }
};

# Try to load Google jQuery.
require(['http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js'],
    functionThatDependsOnJquery);

As far as I know, requirejs is usually used like this:

require(['http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js'],
    function() {
        // Code that depends on jQuery here.
    }
);

Function that depends on jQuery will be called only when jQuery is loaded. But if jQuery fails to load, code that depends on it will never be executed.

Since you want to try and use local jQuery in this case, you should probably catch the script load timeout error and try to load jQuery from another source. (But note that timeout errors are slow.)

There's little information on error handling in docs:

To detect errors, you can override require.onError() to get errors. The error object passed to the onerror function will contain two properties if it is a timeout issue:

  • requireType: value will be "timeout"
  • requireModules: an array of module names/URLs that timed out.

I think, the code may look like this (not tested):

var functionThatDependsOnJquery = function() {
    // Code that depends on jQuery here.
};

require.onError = function(err) {
    # If we have Google jQuery load timeout...
    if (err.requireType = "timeout" &&
        err.requireModules.indexOf('http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js') != -1
        ) {
        # ...we'll try to load it from another source.
        require(['/scripts/localJquery'], functionThatDependsOnJquery);
    }
    # We have another script load timeout, so we just throw an error
    # as requirejs normally does.
    else { throw err; }
};

# Try to load Google jQuery.
require(['http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js'],
    functionThatDependsOnJquery);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文