最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

internet explorer - Javascript: IE event.preventDefault is null or not an object - Stack Overflow

programmeradmin2浏览0评论

My code works fine in FF/Chrome/Everything except IE(failed in both 7/8, didn't bother going furthur down). Due to restrictions, I cannot use jQuery and am hard-coding the Javascript. The issue is with IE, I am getting "preventDefault is null or not an object". Hoping one of you has the answer, and here's relevant code:

AddEvent Method:

function addEvent( obj, type, fn ) {  
  if ( obj.attachEvent ) {  
    obj['e'+type+fn] = fn;  
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}  
    obj.attachEvent( 'on'+type, obj[type+fn] );  
  } else  
    obj.addEventListener( type, fn, false );  
};

Event handler throwing error:

function mousedown(e){  
  if(e.preventDefault){  
    e.preventDefault();  
  } else {  
    e.returnValue = false;  
    e.cancelBubble=true;  
  }
  //Processing   
};

Also DOCTYPE and charset are both set on the calling HTML page. Any ideas would be appreciated. The error is thrown in the if statement for the mousedown method.

EDIT:

Due to the fact that grabbing window.event did "fix" the main issue, I discovered the problem was a different section of code. Basically I am adding a element ontop of a pre-placed element, and then registering mousedown/mousemove events to that div. When firing the event on the <div>, THAT'S where the error is thrown.

Could this be something due to the fact that I have events registered to 2

rect = document.createElement('div');  
rect.className='square';  
rect.id='chooser_rectangle';  
rect.style.left=initx+'px';  
rect.style.top=inity+'px';  

addEvent(rect,"mousemove",mousemove);  
addEvent(rect,"mousedown",mousedown);  

My code works fine in FF/Chrome/Everything except IE(failed in both 7/8, didn't bother going furthur down). Due to restrictions, I cannot use jQuery and am hard-coding the Javascript. The issue is with IE, I am getting "preventDefault is null or not an object". Hoping one of you has the answer, and here's relevant code:

AddEvent Method:

function addEvent( obj, type, fn ) {  
  if ( obj.attachEvent ) {  
    obj['e'+type+fn] = fn;  
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}  
    obj.attachEvent( 'on'+type, obj[type+fn] );  
  } else  
    obj.addEventListener( type, fn, false );  
};

Event handler throwing error:

function mousedown(e){  
  if(e.preventDefault){  
    e.preventDefault();  
  } else {  
    e.returnValue = false;  
    e.cancelBubble=true;  
  }
  //Processing   
};

Also DOCTYPE and charset are both set on the calling HTML page. Any ideas would be appreciated. The error is thrown in the if statement for the mousedown method.

EDIT:

Due to the fact that grabbing window.event did "fix" the main issue, I discovered the problem was a different section of code. Basically I am adding a element ontop of a pre-placed element, and then registering mousedown/mousemove events to that div. When firing the event on the <div>, THAT'S where the error is thrown.

Could this be something due to the fact that I have events registered to 2

rect = document.createElement('div');  
rect.className='square';  
rect.id='chooser_rectangle';  
rect.style.left=initx+'px';  
rect.style.top=inity+'px';  

addEvent(rect,"mousemove",mousemove);  
addEvent(rect,"mousedown",mousedown);  
Share Improve this question edited Nov 25, 2012 at 22:53 MrWhite 46k8 gold badges64 silver badges87 bronze badges asked Sep 9, 2010 at 14:53 avrxavrx 311 gold badge1 silver badge4 bronze badges 6
  • 1 there's got to be something else going on here. your two js methods work fine for a quick demo i set up and tried with IE8. can you most a more plete example with reproducable error? – lincolnk Commented Sep 9, 2010 at 15:08
  • Have you tried checking typeof e and typeof e.preventDefault? – Tim Goodman Commented Sep 9, 2010 at 15:09
  • The example code as is would not cause the error "event.preventDefault is null or not an object". For one you don't reference "event" in your handler, but rather "e". – Crescent Fresh Commented Sep 9, 2010 at 15:11
  • I'm wondering if perhaps the OP did something like if(e.preventDefault()) instead of if(e.preventDefault). I can't see why preventDefault being null should be a problem for using it in an if statement. – Tim Goodman Commented Sep 9, 2010 at 15:17
  • do you have 2 divs with the same id? there's still not enough here to put together an example that demonstrates your problem. – lincolnk Commented Sep 9, 2010 at 16:44
 |  Show 1 more ment

2 Answers 2

Reset to default 6

In IE the event object is not passed as the first argument to an event handler. Instead it is a global variable:

function mousedown(e){
 var evt = e || window.event; // IE patibility
 if(evt.preventDefault){  
  evt.preventDefault();  
 }else{  
  evt.returnValue = false;  
  evt.cancelBubble=true;  
 }
 //Processing   
};

IE has a different event model to other browsers (even IE8). In IE you would call this to do the same thing:

event.returnValue = false;

You need to determine what the browser supports and call the correct method. So first check if event.returnValue is set, if so then call it, otherwise call preventDefault().

发布评论

评论列表(0)

  1. 暂无评论