Javascript event when cursor leaves window
im looking to create a popup which appears once on my homepage when the cursor leaves the browser as if the user was about to click back/close etc, I have the following working fine (as in the popup display) but now I need to somehow drop a cookie so it only happens once, Does anybody know or can tell me how to achiev开发者_如何学JAVAe this? Thanks
<html>
<head>
<script type="text/javascript">
function addEvent(obj, evt, fn) {
if (obj.addEventListener) {
obj.addEventListener(evt, fn, false);
}
else if (obj.attachEvent) {
obj.attachEvent("on" + evt, fn);
}
}
addEvent(window,"load",function(e) {
addEvent(document, "mouseout", function(e) {
e = e ? e : window.event;
var from = e.relatedTarget || e.toElement;
if (!from || from.nodeName == "HTML") {
alert("left window");
}
});
});
</script>
</head>
<body></body>
</html>
Use document.cookie
to set and get cookies.
Here is a tutorial on js cookies
pseudocode:
ON EVENT WINDOW_MOUSEOUT
IF event_cookie.is_present && event_cookie == EVENT_MOUSEOUT THEN
BREAK
ELSE
add_cookie(EVENT_MOUSEOUT)
do_whatever_you_need_to_do_on_first_event()
END IF
END EVENT
精彩评论