Thursday, February 7, 2008

Detecting the javascript file/library has been loaded yet?

For example, you can detect whether the google maps script has been loaded already at run time. Remember, one of the most interesting aspect of browser-based JavaScript is that all globally scoped variables are actually just properties of the window object.
This means any object that has been loaded successfully can be detected using square brace notation of the global window object.

if(window['GMap2']) {
var map = new GMap2('map_div');
}
else {
//see the previous post to load the google maps script after the page has been loaded.
}


You can apply this to any external javascript objects.

No comments:

Subscribe in a Reader