I'm trying to duplicate the tabs demo from the jQuery website. The sample page I'm working with is: http://yazminmedia.com/playground/jquerytabs.html
I'd like to use the Google Code Repository to take advantage of the caching. I have the following code in the head of my page:
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1");
google.load("jqueryui", "1");
$(function() {
$("#tabs").tabs();
});
</script>
However, I keep getting the following error - "$ is not defined" for the following line:
$(function() {
Does anyone have any idea what might be going on that is causing the error?
Thanks!
google.load
injects a script element right after itself. So the order in which script tags appear is:Since the usage of
$
appears before jQuery is included in document order,$
will not be defined in the second step.A solution is to break apart the script tags, so that
google.load
statements appear in their own script tags. So instead if you replace your code with:The order of script tags in the document now will be:
Note that the script element containing your jQuery code now appears after jQuery, so your code should work and
$
orjQuery
should be defined from that point thereon.However, instead of relying on the behavior of
google
's loading order, a better solution is to either use the direct links for the libraries, or use a callback.Or, use the onLoad callback:
For some reason I got the same error
I got away with it by adding this script to every page
then using