Bing Maps v7 Lazy-Loading and the OnScriptLoad parameter

One positive thing that seems to have come out of the confusion regarding the recent Bing Maps v7 AJAX update has just been mentioned in a forum post by Chris Pendleton, in which he suggests a possible resolution to the lazy-loading errors (“xxx is undefined” / “yyy is not a Constructor function” etc.) is to make use of the OnScriptLoad parameter.

This parameter, which had been present throughout the v6.x versions of Bing Maps, was conspicuous by its absence in v7 and, despite several comments regarding its whereabouts, did not appear anywhere in the documentation for the v7 API. Based on Chris’ comments, it appears that this parameter has in fact always been present in v7, but accidentally omitted from the documentation. So, until that gets corrected, you can refer to the v6.3 documentation, taken from here: http://msdn.microsoft.com/en-us/library/cc980837.aspx

“onScriptLoad is a string parameter that specifies the name of a JavaScript function to call when the map control script is loaded. The name must contain only alphanumeric characters. The CSS and tiles will not be loaded when this function is called. (This parameter is useful for loading the map control script from an UpdatePanel in ASP.NET Ajax. Users can call Sys.Application.notifyScriptLoaded from their callback to tell ASP.NET Ajax the script has loaded.)”

Not only does this parameter provide an apparent workaround for some of the issues currently being experienced with the v7 control, but it also provides a useful method for any other functions in the future that you want to execute only after you are certain that the library has been loaded (note that this is not the same as when the map has been loaded into the page). To take the example from my last post, you can therefore safely create an instance of the Microsoft.Maps.Location class by calling the tryCreateLocation function from onScriptLoad callback, as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
  <title>Bing Maps v7 Initialisation Bugs</title>
  <script src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0&amp;onscriptload=tryCreateLocation" type="text/javascript"></script>
  <script type="text/javascript">
    function tryCreateLocation() {
      try {
        var MyLocation = new Microsoft.Maps.Location(51, -0.15);
        alert(MyLocation);
      }
      catch (e) {
        alert(e);
      }
    }
  </script>
</head>
<body></body>
</html>
This entry was posted in Bing Maps and tagged . Bookmark the permalink.

9 Responses to Bing Maps v7 Lazy-Loading and the OnScriptLoad parameter

  1. I actually tried using the “onscriptload” parameter a couple months ago and it didn’t work correctly in v7. Maybe they’ve fixed it in this latest release that broke other stuff?

    • Nate Irwin says:

      Yep, I currently have an open ticket with Microsoft about the “onscriptload” parameter. In short, they say it works, but I’ve only been able to get it to work consistently in certain scenarios with certain browsers. This is a real problem that, as far as I can tell, they aren’t currently acknowledging.

      • alastaira says:

        Hmmm, so there might be a little bit more backstory than simply they “forgot” to include onscriptload in the documentation… still, there are now multiple posts on the forum from Hemant Goyal (MS) recommending its use. So, either it’s fixed, or whatever is wrong with it is not as bad as the current alternative!

  2. hemgoy says:

    Could you try one more time? Below way to use onscript should work for all browsers:

    var map = null;
    var source = “http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0&onscriptload=GetMap”;
    var script = document.createElement(“script”);
    script.setAttribute(“type”, “text/javascript”);
    script.setAttribute(“src”, source);
    document.body.appendChild(script);function GetMap() {
    map = new Microsoft.Maps.Map(document.getElementById(‘myMap’), null);
    }

    • Brian Norman says:

      hemgoy this only seems to work if the script tag is within the body tag, not the head. Is that expected? Either way I agree this is not the way onscriptload used to function and seems more a hack than the correct way

  3. alastaira says:

    Looking back through some old documentation I wrote when first upgrading a v6.x site to v7.x, I explicitly noted that the onscriptload parameter didn’t work, and several other developers have noted the same to me.
    So I think Chris Pendleton was mistaken – the reason why onscriptload isn’t documented in the method reference, or otherwise used in any MS examples, is because it wasn’t present (or not functioning) until recently…

  4. Nate Irwin says:

    Just to make sure we’re clear: I just tested the onscriptload parameter again, and it still is not working for me. I know this isn’t a support forum, but I wanted to make sure this is clear.

    I still have an open ticket with Microsoft on this issue, and I’ll go and post this information on the Bing Maps forum.

  5. The onscript parameter was working for me, but it has stopped working reliably. It seems to be particularly flaky in IE9.

    I’ve switched to using $(window).load(initMap) where initMap is my setup function and it seems to be much happier.

  6. This was super helpful thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s