Javascript error: TypeError: 'null' is not an object (evaluating 'event.target') on safari - Sta

I have difficulty on integrating my JavaScript syntax. My code is working on Internet Explorer (IE). Ho

I have difficulty on integrating my JavaScript syntax. My code is working on Internet Explorer (IE). However, I encountered a JavaScript error when running it on Safari.

This is my test code:

document.onmouseup = function hideaddrspopup () {   
    if (event.srcElement.id != 'fieldName') {   
        alert(event.srcElement.id);
    }
}

I tried something like:

document.onmouseup = function hideaddrspopup() {    
    if (event.srcElement.id != 'fieldName' || event.target.id != 'fieldName') { 
        alert(event.srcElement.id);
        alert(event.target.id);
    }
}

But still an error is appearing on the console:

'TypeError: 'null' is not an object (evaluating 'event.target')'

I am aware that event.scrElement is only working in IE. How to make it work on the other browsers?

I have difficulty on integrating my JavaScript syntax. My code is working on Internet Explorer (IE). However, I encountered a JavaScript error when running it on Safari.

This is my test code:

document.onmouseup = function hideaddrspopup () {   
    if (event.srcElement.id != 'fieldName') {   
        alert(event.srcElement.id);
    }
}

I tried something like:

document.onmouseup = function hideaddrspopup() {    
    if (event.srcElement.id != 'fieldName' || event.target.id != 'fieldName') { 
        alert(event.srcElement.id);
        alert(event.target.id);
    }
}

But still an error is appearing on the console:

'TypeError: 'null' is not an object (evaluating 'event.target')'

I am aware that event.scrElement is only working in IE. How to make it work on the other browsers?

Share Improve this question edited Feb 2, 2016 at 7:18 Pᴇʜ 57.8k10 gold badges55 silver badges75 bronze badges asked Feb 2, 2016 at 6:45 JarichJarich 3372 gold badges9 silver badges25 bronze badges 1
  • event.srcElement was an IE invention. To be patible with IE < 9, use even.target || event.srcElement. – RobG Commented Feb 2, 2016 at 7:18
Add a ment  | 

3 Answers 3

Reset to default 4

According to MDN:

This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large inpatibilities between implementations and the behavior may change in the future.

Event.srcElement is a proprietary alias for the standard Event.target property. It is specific to old versions of Microsoft Internet Explorer.

So just avoid using event.srcElement instead you should use event.target.

As Bhojendra mentioned don't use srcElement. Check for object null or not before accessing id or target property as below.

document.onmouseup = function hideaddrspopup () 
{   

if (e && e.target && e.target.id!='fieldName')
  {   
    alert(e.target.id);
  }
else if(e && e.srcElement && e.srcElement.id!='fieldName')
  {
    alert(e.srcElement.id);
  }
}

Updated with else if to support older IE browser. target should support it too but you could test without else part if it is necessary or not.

You're missing the event variable in your function

document.onmouseup = function hideaddrspopup (event) 
{   

   if (event.srcElement.id != 'fieldName' || event.target.id != 'fieldName')
   {   
     alert(event.srcElement.id);
     alert(event.target.id);
   }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745189092a4615777.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信