How do websites know you're about to close the tab, and pop up an advertisement?

Occasionally, when I move my mouse cursor up to where my tabs are, a website will pop up with something to try and entice me to stay-- an advertisement for other content they have on their site, or a sale offer, or something.

How does the website know where I’ve moved my mouse cursor to?

Because cursor movements are events that can be tracked and trigger actions.

Telemark’s nailed it. It’s trivial to get your mouse coordinates.

Certain browsers (just tested on Firefox) accept CTRL-F4 to close the current tab.

Yep, try out this site that shows your mouse coordinates in real time.

for Chrome users, it’s both CTRL + W and CTRL + F4

I got pretty weirded out the first coupla times this happened to me. Out of nowhere, a big square in the middle of the screen pleading, “Please don’t leave!”.

And I wasn’t necessarily leaving, just going elsewhere to check something for a moment. Once I figured out what was going on, I moved the mouse very slowly to the edge, just from curiosity to see at which point it will consider me gone.