Dojo Goodness, Part 7 (Injecting Dojo After Page Load)

by Matthew Russell

On a recent consulting gig, a client had the requirement that a JavaScript deliverable needed to run in a self-enclosed script tag that would be arbitrarily placed within the body of a page. In other words, I needed to deliver a JavaScript file such that the following code snippet would work:


<!-- somewhere in the page... -->
<div id="specialContainer">
<script type="text/javascript" src="foo.js"></script>
</div>
<!-- ... -->


So, in the end, it's a pretty routine chore. A special container needs to exist at an arbitrary place in the page, the self-enclosed script tag will do some DOM building within it, and all of the magic happens therein. Well, hopefully, it goes without saying that I wanted to streamline the time it took me to complete this task with the help of Dojo.

2 Comments

Xapp
2008-06-30 07:03:48
You might want to add checks to see if dojo is already loaded and if so, which version is loaded.
ptwobrussell
2008-06-30 15:32:08
Hey Xapp - I haven't given this a tremendous amount of thought, but in general, Dojo has a "first Dojo wins" policy, which basically means that if more than one script tag that includes Dojo is in the page, the first one is the one that bootstraps the toolkit. (You can read about it in dojo/_base/_loader/hostenv_browser.js.) I would assume that this holds true for afterOnLoad use cases as well, so to that end, an explicit check may not be necessary. Having said that, I'm sure there is at least one corner case where it probably is necessary....probably some sort of corollary to Murphy's Law.