开发者

Javascript event when cursor leaves window

开发者 https://www.devze.com 2023-03-15 14:59 出处:网络
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 d

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号