为 head.js 创建函数在不同浏览器中会出现相当大的错误

发布于 2024-10-08 12:17:08 字数 1583 浏览 1 评论 0原文

我正在为 head.js ( http://headjs.com) 构建一个函数,它将加载脚本到其中与 jQuewy (http://jquewy.com) 库的方式相同。但是,我在使用不同的浏览器时遇到了一些非常疯狂的错误。期望的结果是获得有关已加载 jQuery 版本的警报:

 (function() {
  head.js(
   jquewy_get("","jquery","ui")
  );

  head.ready(function(){
   alert($.fn.jquery);
  });

  //Takes arguments and sets them so that it 
  //loads the urls in headjs
  function jquewy_get(){
   var ret = new Array();
   var arg = arguments[0];

   var rand = Math.floor(Math.random()*999*999);
   for (var i = 1; i < arguments.length; i++){
    var arg = arguments[i];
    ret[i] = 'hxxp://jquewy.com/dev/headjs/?name='+arg+'&rand='+rand;
   }
   return ret;
  }

 })();

在 Firefox 中,我得到:

第 39 行 $ 未定义

但更奇怪的是,脚本标签已加载 (),但 Firebug 说它有:

无法加载源:hxxp://jquewy.com/dev/headjs/?name=ui&rand=717786

这显然解决得很好。在 Chromium 中,它甚至不加载脚本标签,而且我还没有费心去查看任何其他浏览器,因为如果它在这两个浏览器中不起作用,那么它很可能在其他任何地方都不起作用。

您可以在此处查看源代码: http://jquewy.com/dev/static/ headjs/test/

编辑:对于那些想知道这一点的人,重点是您不需要记住 url - 我正在移植我构建的另一个名为 jQuewy 的服务,它允许通过根据名称加载库来快速构建原型。我的服务将自动获取最新的源并将它们自动嵌入到页面中。 head.js 是另一个执行类似操作的脚本(但更流行),因此我想让我的服务与其兼容,以便最终用户可以选择加载脚本的方式。

编辑#2:我的链接中的硬编码似乎会产生相同的错误。我的服务器使用重定向来指向正确的脚本。这种行为正常吗?是否有独立于浏览器的解决方法?

I'm building a function for head.js ( http://headjs.com) that will load scripts into it in much the same way as the jQuewy ( http://jquewy.com) library does. However, I'm getting some pretty crazy errors with different browsers. The desired result is to get an alert with the version of jQuery loaded:

 (function() {
  head.js(
   jquewy_get("","jquery","ui")
  );

  head.ready(function(){
   alert($.fn.jquery);
  });

  //Takes arguments and sets them so that it 
  //loads the urls in headjs
  function jquewy_get(){
   var ret = new Array();
   var arg = arguments[0];

   var rand = Math.floor(Math.random()*999*999);
   for (var i = 1; i < arguments.length; i++){
    var arg = arguments[i];
    ret[i] = 'hxxp://jquewy.com/dev/headjs/?name='+arg+'&rand='+rand;
   }
   return ret;
  }

 })();

In Firefox, I get:

$ is undefined on line 39

But even weirder, the script tag is loaded (<script type="text/javascript" src="http://jquewy.com/dev/headjs/?name=ui&rand=717786"></script>), but Firebug says that it has:

Failed to load source for: hxxp://jquewy.com/dev/headjs/?name=ui&rand=717786

Which obviously resolves fine. In Chromium it doesn't even load the script tags, and I haven't bothered to look at any other browsers yet because if it doesn't work in those two then chances are it won't work anywhere else.

You can take a look at the source here: http://jquewy.com/dev/static/headjs/test/

Edit: for those wanting to know the point of this, the point is so that you don't need to remember urls - I'm porting another service I've built called jQuewy, which allows for fast prototyping by loading libraries based on their name. My service will automatically fetch the latest sources and embed them in the page automatically. head.js is another script that does a similar thing (but is much more popular), so I want to make my service compatible with it, so that end users can choose how they load their scripts.

Edit #2: Hardcoding in my links seems to create the same errors. My server uses redirects to point to the right scripts. Is this behaviour normal, and is there a workaround that will be browser independent?

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

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

发布评论

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

评论(1

回忆追雨的时光 2024-10-15 12:17:12

由于未设置 jQuery 的 $ 别名,因此发生错误。
脚本正常加载。您可以通过拨打电话等方式进行检查

alert(jQuery('#nav').html());

。有用。
因此您需要自己设置别名。例如:

window.$ = jQuery;
alert($('#nav').html());

如您所见,没有错误。

顺便说一句,由于 $alias 可以设置任何库,因此您必须提供冲突解决机制。正如 jQuery.noConflict() 中所做的那样。

An error occurs due to the fact that the $ alias for jQuery is not set.
Script loaded normally. You can check it by calling

alert(jQuery('#nav').html());

for example. It works.
Thus you need to set alias yourself. For example:

window.$ = jQuery;
alert($('#nav').html());

no error as you can see.

BTW, since $ alias can be set any library, you must provide a mechanism for conflict resolution. As is done in jQuery.noConflict().

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