Load jQuery through Google API in Rails using HAML?
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, 开发者_如何学Pythonbut 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?
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'
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");
精彩评论