You can't call document.write() in the onload event. The only time you can
call document.write() is while the document is still loading, not after it
is done loading.

Note that the Maps API itself uses document.write(), so even if you changed
your loadScript() function to use a dynamic script element (which does work
in the onload event) it would still fail.

-Mike

On Sun, Jun 6, 2010 at 9:23 PM, Acaz Souza <[email protected]> wrote:

> Hi, i'm trying to load dynamically the google maps api javascript and
> i'm failed.
>
> If i load dynamically using document.write out of window.load event,
> is work fine.
> But if i load dinamically in the window.load event, is fail, the page
> get a error and redirect.
>
> This is my code:
>
> window.onload = function() {
> MEGASENA.utilities.loadScript('http://maps.google.com/maps/api/js?
> sensor=false', function() {
>            MEGASENA.googleMaps.buildMap('map_canvas', function() {
>                MEGASENA.googleMaps.addMarker('Ipatinga - MG -
> Brasil');
>                MEGASENA.googleMaps.addMarker('São Paulo - SP -
> Brasil');
>                MEGASENA.googleMaps.addMarker('Rio de Janeiro - RJ -
> Brasil');
>                MEGASENA.googleMaps.addMarker('Barbacena - MG -
> Brasil');
>            });
>        })
> }
>
> ....
>
>
> loadScript = function(url, callback) {
>                var scriptId = MEGASENA.utilities.newKey();
>
>                document.write('<' + 'script id="' + scriptId + '"
> src="' + url + '"' + ' type="text/javascript"><' + '/script>');
>
>                var script = document.getElementById(scriptId);
>
>                if (script.readyState) { //ie
>                    script.onreadystatechange = function() {
>                        if (script.readyState == 'loaded' ||
> script.readyState == 'complete') {
>                            script.onreadystatechange = null;
>                            if (typeof callback == 'function')
> callback();
>                        }
>                    };
>                } else { //others
>                    script.onload = function() {
>                        if (typeof callback == 'function') callback();
>                    };
>                }
>            }
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google Maps API" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-maps-api%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-maps-api?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google Maps API" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-maps-api?hl=en.

Reply via email to