Opened 8 years ago

Closed 8 years ago

#13970 closed defect (invalid)

'dojo is not defined' issue caused by dojo loader

Reported by: hbaaron Owned by: Rawld Gill
Priority: high Milestone: tbd
Component: Loader Version: 1.6.1
Keywords: Cc:
Blocked By: Blocking:

Description

This happens to SDK version only. When using a release version, it won't happen.

How to reproduce

Deploy dojo 1.6 SDK version to your site, refer it as http://example.com/dojo/dojo.js later. Write a Greasemonkey user script, for example, dojo.user.js, with the following line:

Create script tag in head var script = document.createElement("script"); script.src = dojoUrl; document.getElementsByTagName("head")[0].appendChild(script);

Create link style in head var link = document.createElement("link"); link.rel = "stylesheet"; link.type = "text/css"; link.href = dojoStyleUrl; document.getElementsByTagName("head")[0].appendChild(link);

unsafeWindow.addEventListener("load", function(event) {

document.body.className += (" tundra"); console.log(unsafeWindowdojo?); this line will complains unsafeWindowdojo? is not defined

});

Root cause: Under Greasemonkey context, greasemonkey executes the above statements after the whole page is parsed and about to displayed to end user. At this time, any calls to document.write() won't cause output to the document. If you need to insert something to the document, you have to call DOM APIs. However, in the dojo.js, it use document.write() to load

dojo/_base/_loader/bootstrap.js, dojo/_base/_loader/loader.js, dojo/_base/_loader/hostenv_browser.js, dojo/_base.js

So in the end, these scripts are not loaded.

To verify the above assumption, modify the Greasemonkey user scripts as the following:

var script = document.createElement("script"); script.src = dojoUrl; document.getElementsByTagName("head")[0].appendChild(script);

var tmps = [

"http://example.com/dojolib/dojo/_base/_loader/bootstrap.js", "http://example.com/dojolib/dojo/_base/_loader/loader.js", "http://example.com/dojolib/dojo/_base/_loader/hostenv_browser.js", "http://example.com/dojolib/dojo/_base.js"

];

for (var x in tmps){

var _script = document.createElement('script'); _script.src = tmps[x]; document.getElementsByTagName('head')[0].appendChild(_script);

}

Create link style in head var link = document.createElement("link"); link.rel = "stylesheet"; link.type = "text/css"; link.href = dojoStyleUrl; document.getElementsByTagName("head")[0].appendChild(link);

unsafeWindow.addEventListener("load", function(event) {

document.body.className += (" tundra"); console.log(unsafeWindowdojo?); this line is ok now

});

Now you won't get any complain.

Change History (2)

comment:1 Changed 8 years ago by bill

Component: GeneralLoader
Owner: set to Rawld Gill

comment:2 Changed 8 years ago by ben hockey

Resolution: invalid
Status: newclosed

this is the expected behavior for earlier versions of dojo up to and including 1.6.x. you can only dynamically insert a built version of dojo and it is documented in dojo's reference guide (http://dojotoolkit.org/reference-guide/djConfig.html#other-options), source code (http://bugs.dojotoolkit.org/browser/dojo/tags/release-1.6.1/dojo/_base/_loader/bootstrap.js#L78) and other places (eg http://dojotdg.zaffra.com/tag/afteronload/).

the good news is that all of this changes with dojo 1.7 and now you can dynamically insert a source version of dojo without issue.

i'll close this ticket but feel free to reopen if i've misunderstood.

Note: See TracTickets for help on using tickets.