Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to detect when the mouse is over the browser tab?

I was assigned a task to make a script which will generate a pop-up window when the mouse cursor goes over the current opened browser tab.

Something like this demo : http://demo.exitmonitor.com/ but here the pop-up appears always when the mouse leaves the top part of the web page.

I need to generate this window exactly when the mouse is over my current active browser tab.

Is it possible to do this with javascript?

like image 465
Vasil Hirstoff Avatar asked Apr 20 '15 12:04

Vasil Hirstoff


People also ask

How do you check if the mouse is over an element?

You can simply use the CSS :hover pseudo-class selector in combination with the jQuery mousemove() to check whether the mouse is over an element or not in jQuery. The jQuery code in the following example will display a hint message on the web page when you place or remove the mouse pointer over the DIV element's box.

How do I detect if a browser window is closed or refreshed?

A tab or window closing in a browser can be detected by using the beforeunload event. This can be used to alert the user in case some data is unsaved on the page, or the user has mistakenly navigated away from the current page by closing the tab or the browser.

How do you check if a browser tab is currently active or not?

The Page Visibility API: It lets the developers know if the current tab is currently active or not. When the user switches to another tab or minimizes the window, then the pagevisibilitychange event gets triggered. This API added these properties to the document object. document.


1 Answers

I believe you need mouseleave event:

<script>
        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, "mouseleave", function (e) {
                e = e ? e : window.event;
                var from = e.relatedTarget || e.toElement;
                if (!from || from.nodeName == "HTML") {

                    //.... do_this
                }
            });
        });
    </script>
like image 154
Cem Sultan Avatar answered Sep 30 '22 19:09

Cem Sultan