Jay Elrod
Jay Elrod

Reputation: 738

Referencing Firefox event objects via JavaScript

I am still using Google maps v2, and I am having trouble getting the event object from Firefox when a google event fires. Ultimately, I need the event object so that I can retrieve information about keys being pressed when the click event of a particular overlay fires. Here is some code:

GEvent.addListener(polys[i], "click", function(e) {
    var evtObj = window.event? event : e;
    for (var i=0; i<polys.length; i++) {
        if ( this == polys[i] ) {
            if (evtObj.ctrlKey) {
                map.closeInfoWindow();
                clickCounty(labels[i]);
            }
            other stuff to happen....
        }
});

This works fine in IE and Chrome. The variable e passed into the function is the lat/long of the click event. So how would I reference the Firefox event object rather than the Google event lat/long..?

Upvotes: 0

Views: 352

Answers (1)

glortho
glortho

Reputation: 13200

There is no great way to do this. I recommend binding an event to the DOM version of the polys[i] object (usually referenced in something like polys[i].Zk) instead of using GEvent.addListener. It may be easier to use the rendered object and attach to that after the map has loaded, rather than looking for it in polys[i].Zk.

For example, if your object is an img marker, you could do something like this (using jQuery for shorthand, even though I know you haven't tagged with jQuery):

$('#map-box').on('click', 'img', function(evtObj) {
  for (var i=0; i<polys.length; i++) {
    if ( this == polys[i].Zk ) {
      if (evtObj.ctrlKey) {
        map.closeInfoWindow();
        clickCounty(labels[i]);
      }
      other stuff to happen....
    }
  ...
});

Of course, you'd probably want to be a little more granular in your selection, and you lose the lat/lon normally passed in, but you can access those elsewhere if you really need them (e.g. you could keep the addListener binding and cache the coordinates to use across bindings).

Upvotes: 1

Related Questions