Modernizr/ yepnope 执行顺序 WRT jQuery +插件故障排除

发布于 2025-01-02 16:38:11 字数 1204 浏览 2 评论 0原文

今天我正在研究 Modernizr polyfill 的加载,但遇到了一些麻烦。我喜欢并行加载 jQuery 的想法,因此将 CDN URI(带有本地回退)放在顶部。然后我开始加载一些 polyfill,这些都是我在发现 Modernizr 之前使用的 jQuery 插件。

问题是,插件较小,并且在 jQuery 完成和执行之前加载,因此“$”和“jQuery”对象最终都未定义。 yepnope 文档说,尽管并行下载,加载程序应该尊重依赖脚本的执行顺序,但严格遵循示例语法仍然会产生错误。

你们都可以看一下我下面的代码,看看我想要什么吗?该代码片段直接位于 Modernizer 最小化代码下方;它位于我的 中。仅供参考,有大量的效果 jQuery 代码也会在我的 底部加载,但我希望在设置 'complete:' 函数之前,polyfills 能够工作。他们。

    Modernizr.load([
  {
    load: '//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js',
    complete: function () {
      if (!window.jQuery)
            Modernizr.load('js/jquery.1.7.1.min.js');

    }
  },
  {//Border Radius
    test : Modernizr.borderradius,
    nope : 'js/polyfills/jquery.curvycorners.js'
  },
  {//Text Shadow
    test: Modernizr.textshadow,
    nope: 'js/polyfills/jquery.textshadow.min.js',
    complete: function(){
        $(function(){
            $("h2").textShadow(); 
            $("h3").textShadow();
            $("a.facebook").textShadow();
            $("a.twitter").textShadow();
        });
    }
  },
  {//Box Shadow
    test: Modernizr.boxshadow,
    nope: 'js/polyfills/jquery.boxshadow.js'
  }
]);

I'm getting my head around Modernizr polyfill loading today and running into some trouble. I like the idea of loading jQuery in parallell and so have the CDN URI (with a local fallback) up top. Then I get in to loading some polyfills which are all jQuery plugins that I was using prior to discovering Modernizr.

The trouble is, the plugins are smaller and load before jQuery is done and exocuted, so '$' and 'jQuery' objects both wind up undefined. The yepnope docs say that the loader is supposed to respect the execution order of dependant scripts despite parallell downlaoding, but following the sample syntax to the letter still produces the error.

Can y'all have a look at my code below and see where I'm wanting? This snippet lies directly under the Modernizer minimized code; which is situated in my <head>. FYI, there is a slew of effects jQuery code that also get loaded at the bottom of my <body>, but I want the polyfills to work before I go setting up the 'complete:' function for them.

    Modernizr.load([
  {
    load: '//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js',
    complete: function () {
      if (!window.jQuery)
            Modernizr.load('js/jquery.1.7.1.min.js');

    }
  },
  {//Border Radius
    test : Modernizr.borderradius,
    nope : 'js/polyfills/jquery.curvycorners.js'
  },
  {//Text Shadow
    test: Modernizr.textshadow,
    nope: 'js/polyfills/jquery.textshadow.min.js',
    complete: function(){
        $(function(){
            $("h2").textShadow(); 
            $("h3").textShadow();
            $("a.facebook").textShadow();
            $("a.twitter").textShadow();
        });
    }
  },
  {//Box Shadow
    test: Modernizr.boxshadow,
    nope: 'js/polyfills/jquery.boxshadow.js'
  }
]);

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

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

发布评论

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

评论(1

〆凄凉。 2025-01-09 16:38:11

经过大量研究和实验,看起来这是概念层面上的 yepnope 问题。由于 jQuery 的加载时间线,插件总是会在它准备好之前寻找它。虽然 jQuery 测试的完整函数中的嵌套 load() 是破解它的一种方法,但您会在相当长的时间内看到用户将看到没有填充的页面。虽然这对于表单验证来说可能没问题,但当您像我在本示例中那样填充 border-radiustext-shadow 时,它看起来就像 FOUC 一样糟糕。

我发现的最佳解决方案是通过 HTML5 样板方法加载 jQuery:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
<script>window.jQuery || document.write('<script src="js/libs/jquery-1.7.1.min.js">\x3C/script>')</script>

然后将 Modernizr.load() 放入 $(document).ready(); 中。虽然这抵消了 Modernizr 的大部分并行加载优化优势,但它保留了 yepnope 渐进增强原则,允许您使用 Modernizr 进行 HTML5 填充(恕我直言,这只是整洁),并确保 jQuery 绝对可用于 polyfill。另一个结果是您可以在完整函数中安全地使用 $() 选择,这很方便。

After a lot of research and experimentation, it looks this is an issue with yepnope at a concept level. Because of the load timeline for jQuery, the plugins will always go looking for it before it is ready. While a nested load() in the jQuery test's complete function is one way to hack it, you're then looking at a decent amount of time that the user will see the page without polyfills. While this might be ok for form validation, it looks as bad as a FOUC when you're polyfilling border-radius and text-shadow the way I am in this example.

The best solution I found was to load jQuery via the HTML5 boilerplate method:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
<script>window.jQuery || document.write('<script src="js/libs/jquery-1.7.1.min.js">\x3C/script>')</script>

Then place a Modernizr.load() in the $(document).ready();. While this offsets most of the parallel-load optimization benefits of Modernizr, it maintains the yepnope progressive enhancement principal, lets you use Modernizr for HTML5 shimming (which is just tidy IMHO), and ensures that jQuery is definitely available for polyfills. An upshot too is that you can safely use $() selection in the complete function, which can be handy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文