Question

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

<script src="http://www.google.com/jsapi"></script>
<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?

Was it helpful?

Solution

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");
    ...
}

OTHER TIPS

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

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.

dojox is practically unmaintained, and will be taken out from dojo-2. There are major problems with most widgets in dojox, there is only a few good.

IMHO dojo should be self-hosted, because there are always things what you need to overwrite - for example, you need some fix in this dojox.gfx.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top