Click here to Skip to main content
15,885,216 members
Articles / Web Development / ASP.NET

Fix for Bing Maps Not Working in Firefox 4

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
2 May 2011CPOL2 min read 16.1K   3   1
A fix for a JavaScript error with Bing Maps in Firefox

Recently I upgraded from Firefox 3.6 to Firefox 4 and while doing regression testing, I noticed that apparently Bing Maps has some issues in Firefox 4. In particular, I saw a JavaScript error that said "p_elSource.attachEvent is not a function":

The problem at this point looks like this:

  1. the error happens in Bing's JavaScript itself; and
  2. it only happens in the Firefox 4 browser. Other browsers like Internet Explorer 8 & 9, Chrome, Safari, Opera and Firefox 3.6 don't have this problem.

Since the page where I saw the error belonged to a web application I started working on, I had to fix the problem. An initial search on the Internet did not bring up anything useful except that a few other people also stepped on this problem. I tried Bing Maps' interactive SDK and it worked all right. That told me the Bing Maps script itself does work but it clearly depends on some other condition that does not necessarily happen on other pages like mine.

What's that condition? I looked at the difference between the Firefox browser and the other browsers. When in Firefox, Bing Maps dynamically loads another JavaScript file atlascompat.js. Apparently that file is required since it contains a definition for the attachEvent function that caused the error and must be loaded first before the main Bing Maps script. So that's the condition I've been looking for! Now the picture got clearer:

  1. the error happens in Bing's JavaScript itself; and
  2. it only happens in the Firefox 4 browser. Other browsers like Internet Explorer 8 & 9, Chrome, Safari, Opera and Firefox 3.6 don't produce that problem;
  3. it happens when atlascompat.js is not present on a page at the moment when the main Bing Maps script is being loaded.

Now why is the error happening on my page and not on the Bing Maps SDK page? Apparently the condition #3 is not satisfied since my page contains a whole bunch of other scripts and the atlascompat.js did not load before the main Bing Maps script due to internal differences that have been introduced in Firefox 4. Now I have a complete picture of the problem and can come up with a solution for it.

The solution is simple: since the Bing Maps itself cannot load the atlascompat.js reliably, I need to help it and just add a reference to the atlascompat.js script on my page before a reference to the main Bing Maps script. This can be easily accomplished with a few lines of code, like below:

C#
if ((Page.Request.Browser.Browser.IndexOf("Firefox") >= 0) && 
         (Page.Request.Browser.MajorVersion == 4))
{
    // add script reference on a page
    // use technique that is suitable for your application
}

In my case, I added the code above into a GetScriptReferences method of an IScriptControl that was responsible for rendering the Bing Maps on the page.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Canada Canada
Alexander Turlov is a professional software development consultant that has been working in IT industry since 1987. His programming experience includes such languages as FORTRAN, Pascal, Basic, C, C++ and C#. He's been working for different industries including but not limited to science, manufacturing, retail, utilities, finance, insurance, health care, education and so on. His area of professional interests is cloud powered rich web applications development with .NET, C#, ASP.NET/MVC and JavaScript. He is working in software development doing architecture, design and development on .NET platform and using Microsoft Visual Studio, Azure and Visual Studio Team Services as his primary tools. He holds a M.Sc. degree in physics and various industry certifications including MCSD.NET, Azure and Scrum.

View my profile on LinkedIn

View my blog

Comments and Discussions

 
Questionnot able go grt atlascompat.js Pin
Prashant Vishnoi8-Aug-16 1:33
Prashant Vishnoi8-Aug-16 1:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.