Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, 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?

like image 337
neezer Avatar asked Dec 17 '25 00:12

neezer


1 Answers

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'
like image 59
cwninja Avatar answered Dec 19 '25 17:12

cwninja