tags:

views:

573

answers:

3

I'm using the following html to load dojo from Google's hosting.

<script src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
<script type="text/javascript">google.load("dojo", "1.1.1");</script>
<script type="text/javascript">
dojo.require("dojox.gfx");
...

This errors out on the requre line with an error like dojox.gfx is undefined. Is there a way to make this work, or does Google not support the dojox extensions?

Alternatively, is there another common host I can use for standard dojo releases?

A: 

I believe that google becomes the namespace for your imported libraries. Try: google.dojo.require.

Oh! And as pointed out below, don't forget to use google.setOnLoadCallback instead of calling your function directly.

William Keller
google.dojo is undefined
Peter Shinners
A: 

A better question is - why would you want to? If you are developing on your localhost then just use a relative path, if you're developing on an internet facing server - stick the dojo files on that.

Also - make sure you're not falling foul of the same origin policy

Richard Franks
Using an external version makes it easier to share single file prototypes with other users for now. Besides, Google sold me with their http://code.google.com/apis/ajaxlibs/documentation
Peter Shinners
+4  A: 

Differently from when you reference the .js files directly from the <script> tag (note that google js api also supports this, see here), google.load is not synchronous. This means that when your code reach google.load, it will not wait for dojo to be fully loaded to keep parsing; it will go straight to your dojo.require line, and it will fail there because the dojo object will be undefined.

The solution (if you don't want to use use the direct <script> tag), is to enclose all your code that references dojo in a start function, and set it will as a callback, by doing:

google.load("dojo", "1.1.1", {callback: start});

function start() {
    dojo.require("dojox.gfx");
    ...
}

or

google.setOnLoadCallback(start);
google.load("dojo", "1.1.1");

function start() {
    dojo.require("dojox.gfx");
    ...
}
Felipe