Search code examples
javascriptinternet-explorerfirefoxlync-2013

Unable to get property 'srcElement' of undefined or null reference OR window.event is undefined in FF


I have implemented a Lync Contact Card Presence as follows:

var email = "[email protected]";
var uuid = guid() + ",type=sip";
IMNRC(email, document.getElementById(uuid));
function guid() {  function s4() { return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1); }
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4(); }

In IE 10 and above it works sometimes, sometimes it throws "Unable to get property 'srcElement' of undefined or null reference".

In FireFox, it throws "window.event is undefined in FF"

I tried to use the following code but it didn't work:

document.addEventListener('onclick',ex.exampl,true);

var ex = { exampl: function(e){ var evt = e || window.event } }

Solution

  • It should be "click" not "onclick". You need to defined the object before you reference it because of hoisting. And you show an error about srcElement, but there is no srcElement in your code.

    var ex = {
      exampl: function(e) {
        var evt = e || window.event,
            target = e.target || e.srcElement;
        console.log(target);
      }
    }
    
    document.addEventListener('click', ex.exampl, true);
    <div>1</div>
    <div>2</div>
    <div>3</div>

    In modern browsers, you really do not need the window check.