0

I'm having trouble with e.preventDefault() for IE(8).

Everything works in Chrome (meaning the execution is correct, and the default action is prevented). However, in IE, the execution is correct but the default action happens as well.

In further investigation I found that whenever I look at the event object, it fails (no error, just exits the handler quietly).

I removed all the code and boiled it down as follows:

google.earth.addEventListener(spot.placemark, 'click', test);

function test(e){
  alert(1);
  e.returnValue = false;
  alert(2);
  if(e.preventDefault) e.preventDefault(); 
  alert(3);
  return(false);
}

So with IE, only the first alert fires. With chrome they all fire. If I reverse alert 2 and alert 3, still only alert 1 fires. Fundamentally - touching e fails.

I also tried using the window.event object instead of relying on the passed value of e.

var e = window.event;

But that had the same effect. Appreciate some pointers. Thank you

Charles
  • 50,943
  • 13
  • 104
  • 142
bruce
  • 1,408
  • 11
  • 33

1 Answers1

0

as answered in event.preventDefault() function not working in IE

function test(e) {
    e.preventDefault ? e.preventDefault() : e.returnValue = false;
}
Community
  • 1
  • 1
Manuel van Rijn
  • 10,170
  • 1
  • 29
  • 52
  • But the very act of looking at e causing a 'no such property' error. I can get round that by using try/catch. But that doesn't help me to actually set the `e.returnValue`. – bruce Oct 21 '11 at 11:21