views:

127

answers:

3

I am trying to understand how to use jQuery when it is loaded using Google CDN's google.load method.

Putting the init code in the setOnLoadCallback function works great but defining functions for event handlers in the markup doesn't seem to work. In the example below, the P button works as expected but the Div button does nothing.

Yes, in this example, I can put the div's onclick handler also in the setOnLoadCallback function but does that mean that all jQuery code has to be there?

Help? Thanks

<p id="p">Content</p><button type="button" id="btn1">P</button>

<div id="div">Div</div><button type="button" id="btn2" onclick="btn2()">Div</button>

<script src="http://www.google.com/jsapi"&gt;&lt;/script&gt;

<script>

function btn2() {
 $("#div").toggle("slow");
}

google.load("jquery", "1.3.2");
google.setOnLoadCallback(function() {
$("#btn1").click(function () {
      $("p").toggle("slow");
});
});

</script>
A: 

One of the key points of JQ is to be unobtrusive thus you shouldnt be using <element onclick="..."></element>. You should always be using $(selector).click(). Furthermore you generally want to have this consolidated in a single $(document).ready();, or in exeternal scripts.

prodigitalson
I agree but I have inherited a lot of that embeds event handlers in the markup :-( and I would like to use jQuery in the event handler's implementation.From the responses so far, it appears that this can **not** be done using the google.load technique, it has to be done using the direct path to the library. So that appears to be the more flexible method, wonder why Google recommends using google.load!
VANJ
Personally I dont use CDN in production. I know the benefits, but I dont like having things out of my control... id rather jsut use a minifier to compress all my css to a singel file and all my js to a single file on the server side before i send. I dont get the caching benefits but i can live with that for better control and a garauntee of availability. Im not all that trusting of the cloud :-)
prodigitalson
I see your point when supporting just one environment. But when you have a dozen test environments, it is a bit of a hassle to update the latest version of a JS framework/library on all these servers, much easier to point to a CDN and let someone else do it. Thanks.
VANJ
Thats what SVN externals or a build system are for :-)
prodigitalson
+2  A: 

Put your Google jsapi script call and google.load at the top of <head> in your document. When run, it will just output

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" 
    type="text/javascript"></script>

where google.load was.

Then put all your jQuery code inside:

$(function() {
    // all your jQuery code here
});

which is shorthand for $(document).ready(function(){ });

carillonator
A: 

Yes, you need to have all your jQuery code inside setOnLoadCallback if you want to load jQuery this way. Until that event fires, there is no guarantee that the jQuery script has loaded, and thus any jQuery code outside that function may be invalid. If you want to define jQuery code outside of that event, you can do it by loading jQuery from Google's CDN URL instead of dynamically by google.load().

The url for jQuery from Google's CDN looks like this:

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

You can just include it in the head of the document as an external script resource as usual. You still get the benefits of the Google CDN because most browsers will have a cached copy of jQuery at that location (assuming they have visited another site that used the Google CDN).

Chris Clark