More Cross-browser Fun

I wrote last week about some of the fun I've been having with the Virtual Earth Map control, including some jokes about how much I love javascript.

Well, much to my chagrin, one of the very helpful folks in the local developer community burst my bubble of pride over my accomplishments by pointing out that the map page I build failed miserably on Firefox.

Now I know that being a Microsoftie, I'm not supposed to like Firefox, or work hard to make my code work well with it, but here's the thing...the project I'm working on is something I want to be available far and wide, and I don't want something like javascript errors to keep someone from using it. So I decided to fix the problems.

One challenge with this is most of my programming has been on the server side, so I'm a little rough around the edges when it comes to javascript and the DOM. Thankfully, there are lots of good resources out there to cope with some of the vagaries of the cross-browser world, including things like how to set the text of an html element in a way that will work in more than just IE.

One of the things I was playing with using the VE map control was a utility page that would take an address, and then map the location and return the latitude/longitude for the address. Not having written client-side code in a while, I innocently tried to write the result out to a div using element.innerText. Well, that worked great in IE, but it's not supported in Firefox. So a few searches later, here's what I found:

var lldiv = document.getElementById('latlong');
var txt = 'The Lat/Long is:' + map.GetCenter();            lldiv.appendChild(document.createTextNode(txt));

So basically, I use document.getElementByID to get a reference to the div that I want to write to, then call appentChild on it to populate the text. I've tested this in IE7 and Firefox 2, but don't have the resources to test it elsewhere at the moment. I'm hoping it will work across other browsers as well...if you're interested in playing with the full code for the page, contact me and I'll be happy to share it.

I'll have more to come on this topic soon.

Technorati Tags: cross browser, javascript, virtual earth