2

Hi I have this code where i tried to get mouse position when i try to unload the page.

Each time i run code and alert gives me unidentified value.

Here is my output,

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script>

var mouse = {x: 0, y: 0};
 
document.addEventListener('mousemove', function(e){ 
    mouse.x = e.clientX || e.pageX; 
    mouse.y = e.clientY || e.pageY 
}, false);

if (window.addEventListener) {  // all browsers except IE before version 9
 window.addEventListener("beforeunload", function (e) {
    var confirmationMessage = "Hi";
    alert(mouse.x + ' : ' + mouse.y);     
     return confirmationMessage;                            
  });
}
else {
    if (window.attachEvent) {   // IE before version 9
     window.attachEvent("beforeunload", function (e) {
           var confirmationMessage = "Hi";
    alert(mouse.x + ' : ' + mouse.y);
     return confirmationMessage;                            
  });

    }
}
</script>
</head>

<body>
  <h1>This is a Heading</h1>
  <p id = "myAns">This is a paragraph.</p>
</body>

</html>

I modified code that you guys suggested. So my problem is then how to find out when user click on close button of browser or close button of tab ?

because I thought that when user clicks out of document then mouse position is 0 and we can tell that user clicked on close button "beforeUnload" is called.

Thanks in advance.

JBaba
  • 590
  • 10
  • 30
  • 4
    Because the unload event isn't a mouse event such as click. The mouse position has no relevance to unload. – James Montagne Mar 30 '15 at 20:08
  • 1
    Right - you only get mouse information when the event itself has something to do with mouse activity. – Pointy Mar 30 '15 at 20:09
  • You could track the mouse all of the time... maybe on an interval and return it's position on unload – Huangism Mar 30 '15 at 20:11
  • http://stackoverflow.com/a/2601273/717383 – James Montagne Mar 30 '15 at 20:12
  • @JamesMontagne but when you try in Ie 8 then it returns position. – JBaba Mar 30 '15 at 20:15
  • 1
    @NaimishViradia IE<9 didn't follow the standard at all. Look at https://developer.mozilla.org/en/docs/Web/API/Event to see all event types and their common properties. Only mouse events (and their subtypes) have the position. – Domino Mar 30 '15 at 20:21
  • @JacqueGoupil I modified the code and look at my problem now. – JBaba Mar 30 '15 at 20:51
  • @JamesMontagne I modified the code and look at my problem now. – JBaba Mar 30 '15 at 20:52
  • @Pointy I modified the code and look at my problem now. – JBaba Mar 30 '15 at 20:56
  • 1
    @NaimishViradia what is the main goal behind learning how the tab was closed - maybe we can go somewhere from there? btw I rarely use the mouse to close a tab, usually just a keyboard shortcut. – Jaak Kütt Mar 31 '15 at 12:58
  • someone please correct me - aren't the tab and browser close buttons outside the e.clientY/X range? – Jaak Kütt Mar 31 '15 at 13:01
  • @JaakKütt i want to do logout when this actions are performed. Because we are using old JSP and Servlet code. – JBaba Mar 31 '15 at 13:22
  • I wouldn't rely on catching an unload to perform a logout - either a visible logout button for a user to see and click or kill the session on the server side after a timeout of inactivity – Jaak Kütt Mar 31 '15 at 13:43

1 Answers1

1

Browsers don't throw mouse events when a tab is closed. I know some browsers have specific tab events, but only to be used by browser extensions.

Detecting that the user closed the page is impossible in JS - all you can detect is if they left the page, which can mean many things (clicked on a link, closed the browser, typed an address, opened a bookmark, pressed Alt+F4...). The mouse position can't be used to determine what the user did to leave the page, because he could be using an alternative form of browser (touch screen, gaming console, command line browser, any browser without a mouse...) and the buttons could be at completely different locations.

You shouldn't rely purely on JS to disconnect a user. If you want to force the user out, use some server code which keeps track of the session ID, and also put an inactivity timer on the server.

Domino
  • 6,314
  • 1
  • 32
  • 58