I have run into the problem, and I can’t seem to find a solution. I’m using the Yahoo Maps API on a page, and it takes forever to load the JavaScript file that needs to be included. So now, I am trying to dynamically include the JavaScript file only if the map is needed, rather than on every page load. Here is the setup (source) I have to do it:

function dhtmlLoadScript(url){
    var e = document.createElement("script");
    e.src = url;
    e.type="text/javascript";
    document.getElementsByTagName("head")[0].appendChild(e);
}

And then onload, I call that function passing it http://api.maps.yahoo.com/v2.0/fl/javascript/apiloader.js?appid=OurId. The page appears to get redirected, and just hangs forever. Now, if I replace that URL with a URL of a script on my localhost, or on my server, or on another server of mine, it works just fine.

Can anyone see somehting I am doing wrong, or point out another way to make this work? It looks like I may have to place the map inside of an iframe, but I want that to be my last option.

HTML Form Builder
Ryan Campbell

Dynamically Include Yahoo Maps by Ryan Campbell

This entry was posted 4 years ago and was filed under Notebooks.
Comments are currently closed.

· 12 Comments! ·

  1. Teevio · 4 years ago

    Nevermind, I didn’t read what you were trying to do properly.

  2. Drew Yeaton · 4 years ago

    Could that be a security related restriction?

  3. Jan BraÅ¡na · 4 years ago

    Your issue aside, I’m afraid it won’t work in Safari — if I’m not mistaken Safari can’t load scripts appended to head via DOM functions.

  4. Andrew · 4 years ago

    I’ve given it a go and I’m coming to the same result. It seems as though the Yahoo! Maps library is waiting for the onload event to fire up the engine, which has already fired so it just sits in idle (that’s just a guess).

    An additional problem I anticipate you’ll run into if this is solveable is the issue of instantiating your map. You’ll have to wait until the scripts actually load in order to call var map = new Map(); I’ll keep chugging because I anticipate needing this at some point.

  5. Justin Palmer · 4 years ago

    Wow, that Yahoo file loads about 32 other Javascript files. I’m sure this has something to do with the load times. You might try and beat the hell out of it with a hammer and use document.write:

    document.write('');
    

    As Kyle suggested on the forum, Safari doesn’t let you muck around with the head of a document.

  6. Justin Palmer · 4 years ago

    Wow, that Yahoo file loads about 32 other Javascript files. I’m sure this has something to do with the load times. You might try and beat the hell out of it with a hammer and use document.write:

    document.write('');
    

    As Kyle suggested on the forum, Safari doesn’t let you muck around with the head of a document.

  7. Dustin Diaz · 4 years ago

    I… don’t… know… Sorry :(

  8. Ryan Campbell · 4 years ago

    Just an update: I tried using document.write also, and a similar problem happens. The page just hangs (this time without the redirect). I’ll keep playing with it and see if I can come up with anything besides an iframe.

    Thanks for the suggetions.

  9. Stephen McCartney · 4 years ago

    I know this doesn’t help you very much, but I recently experienced a very similar problem while trying to add the FlickrBadge script to a page dynamically.

  10. Aaron · 4 years ago

    The v3.0 version of the API now has all the Javascript files combined into one file, which significantly improves load time.

  11. Kevin Hale · 4 years ago

    Oh BLESS THE LORD! Thanks Aaron for the update!

  12. Andrew · 4 years ago

    So despite some updates from v2 -> v3.02, the hanging problem still persists. The generated source for the page is this:

    That script contains this:

    Sub setUpVBCallback(flashId) ExecuteGlobal "Sub " & flashId & _ "_FSCommand(ByVal command, ByVal args)" & _ "FlashProxy.callJS command, args" & vbcrlf & _ "End Sub" End Sub

    However, I still have no idea what’s going on.