Undefined mouse event properties in IE 11

The following code that I use to detect a browser window closing event works in other browsers, but not in IE11. Since this line of code gives me undefined only on IE11 alert(event.clientY+":"+event.clientX); . Anyone can suggest a solution to this problem.

 window.onbeforeunload = function(event) { event= window.event|| event; alert(event); alert(event.clientY+":"+event.clientX); if(event.clientX <0 || event.clientY < 0) { // ajax call to server to nullify the session. window.close(); } }; 
+7
javascript debugging internet-explorer-11 window eventhandler
source share
2 answers

From the MDN beforeunload event, you can see which properties are supported from the event object.

Client X and clientY of the event object are not supported, therefore they are undefined.

This also happens in Chrome and FF, since the onbeforeunload event does not contain such information (positional X and Y)

I checked your code in IE11, Chrome 48, FF 44.

Possible workaround:

 var clientX = 0; var clientY = 0; var scheduled = false; window.onmousemove = function (event) { if (!scheduled) { scheduled = true; setTimeout(function () { event = event || window.event; clientX = event.clientX; clientY = event.clientY; scheduled = false; }, 1000); } } window.onbeforeunload = function (event) { alert(clientY+":"+clientX); if (clientX < 0 || clientY < 0) { // ajax call to server to nullify the session. window.close(); } }; 
0
source share

An event object is defined twice:

 event = window.event || event; 

but the link is not used here:

 if(window.event.clientX < 0 || window.event.clientY < 0) 

therefore IE fails because the code should be:

 if(event.clientX < 0 || event.clientY < 0) 
+1
source share

All Articles