使用 HAML 通过 Rails 中的 Google API 加载 jQuery?

发布于 2024-08-08 02:09:20 字数 1202 浏览 2 评论 0原文

感觉应该很简单,但似乎加载的并不多。

我的 app/views/layouts/application.html.haml 中有这个:

= javascript_include_tag 'http://www.google.com/jsapi'
%script{ :type => "text/javascript", :charset => "utf-8" }
  //<![CDATA[
  google.load("jquery", "1.3.2"); 
  google.load("jqueryui", "1.7.2");
  //]]>
= javascript_include_tag 'application'

...我的 application.js 文件包含一些很好的 'ole jQuery。我已经安装了 JRails 并且我的 jQuery 可以很好地处理库的本地副本,但我想使用来自 Google API 的。

以下是我的浏览器生成的内容:

<script src="http://www.google.com/jsapi.js" type="text/javascript"></script>
<script charset='utf-8' type='text/javascript'>
  <!-- /<![CDATA[ -->
  google.load("jquery", "1.3.2");
  google.load("jqueryui", "1.7.2");
  <!-- /]]> -->
</script>
<script src="/javascripts/application.js?1255040651" type="text/javascript"></script>

我正在使用 Safari 和错误控制台,它报告以下错误:

ReferenceError: Can't find variable: google
ReferenceError: Can't find variable: $

相应地,我的 jQuery 脚本都不起作用。

帮助?

This feels like it should be pretty simple, but not much seems to be loading.

I have this in my app/views/layouts/application.html.haml:

= javascript_include_tag 'http://www.google.com/jsapi'
%script{ :type => "text/javascript", :charset => "utf-8" }
  //<![CDATA[
  google.load("jquery", "1.3.2"); 
  google.load("jqueryui", "1.7.2");
  //]]>
= javascript_include_tag 'application'

... where my application.js file contains some good 'ole jQuery. I have installed JRails and my jQuery works fine with local copies of the libraries, but I want to use the ones from Goolge API.

Here's what my browser generates:

<script src="http://www.google.com/jsapi.js" type="text/javascript"></script>
<script charset='utf-8' type='text/javascript'>
  <!-- /<![CDATA[ -->
  google.load("jquery", "1.3.2");
  google.load("jqueryui", "1.7.2");
  <!-- /]]> -->
</script>
<script src="/javascripts/application.js?1255040651" type="text/javascript"></script>

I'm using Safari and the Error Console, which reports the following errors:

ReferenceError: Can't find variable: google
ReferenceError: Can't find variable: $

Correspondingly, none of my jQuery scripts are working.

Help?

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

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

发布评论

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

评论(2

酷炫老祖宗 2024-08-15 02:09:20

javascript_include_tag 自动在末尾添加 .js。使用 javascript_include_tag 时似乎没有办法解决这个问题。您应该(根据您自己的评论)创建自己的脚本标记:

%script{ :src => 'google.com/jsapi', :type => 'text/javascript', :charset => 'utf-8' }

就我个人而言,我更喜欢跳过 jsapi,并直接引用库,所以只需:

= javascript_include_tag 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'

javascript_include_tag automatically puts a .js on the end. There seems to be no way round this while using javascript_include_tag. You should (as per your own comments) create your own script tag:

%script{ :src => 'google.com/jsapi', :type => 'text/javascript', :charset => 'utf-8' }

Personally I prefer to skip the jsapi, and reference the libraries directly, so just:

= javascript_include_tag 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'
两仪 2024-08-15 02:09:20

作为关于 Haml 的旁注,您可以使用 :javascript 过滤器,而不是手动使用 CDATA 和内容定义脚本标记:

:javascript
  google.load("jquery", "1.3.2"); 
  google.load("jqueryui", "1.7.2");

As a sidenote about Haml, you can use the :javascript filter rather than defining a script tag with CDATA and content manually:

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