8

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?

Arun Bertil
  • 4,598
  • 4
  • 33
  • 59
Vasil Hirstoff
  • 121
  • 1
  • 1
  • 5
  • 2
    Impossible to detect and annoying – epascarello Apr 20 '15 at 12:29
  • 1
    It's not possible. All you can do is attach a `mouseleave` event to the `document` so you know when the mouse leaves the document, but not exactly where outside it that it went. – Rory McCrossan Apr 20 '15 at 12:30
  • 1
    It might be possible in some browsers with an addon written in JS. However, such a thing will turn people absolutely furious. – Siguza Apr 20 '15 at 12:35
  • May be you could track the mouse position, and then see when the cursor gets outside the way that you want to respond to. – Ahmed Apr 20 '15 at 12:36
  • @RoryMcCrossan To get where the mouse left the document, use `MouseEvent.clientX` and `mouseEvent.clientY`: `document.addEventListener("mouseleave", function(e) { console.log(e.clientX, e.clientY); })` – Noble Mushtak Apr 20 '15 at 12:42
  • Ok, but how are you going to determine from that where the mouse is now? Sure they could have left the document from the top, but what if they have their tabs at the bottom of the window? – Rory McCrossan Apr 20 '15 at 12:43
  • This question is asked about once every two weeks. http://stackoverflow.com/questions/3888902/javascript-detect-browser-close-tab-close-browser http://stackoverflow.com/questions/21227383/how-to-detect-browser-window-tab-close-event http://stackoverflow.com/questions/3185105/how-to-detect-the-windownew-tab-close-event – Domino Apr 20 '15 at 12:46
  • I'm not asking about closing the tab but something like "hover" over it. – Vasil Hirstoff Apr 20 '15 at 12:50
  • Do you want the popup to come when the mouse is _on_ the tab or when it _leaves_ the tab? Both are possible. – Noble Mushtak Apr 20 '15 at 12:54

4 Answers4

2

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>
Cem Sultan
  • 79
  • 1
  • 13
1

I assume by "tab" you mean the area highlighted in red:

Firefox tab bar

In all modern browsers a website cannot access anything out of its window, except for APIs explicitly provided to it.
Therefore, there is not way for you to even access the tab bar with just JavaScript.

Whether or not there is at all a way to get access to the tab bar depends on the browser, but it will (most certainly) require a browser addon.

In Chrome, for example, this was not at all possible back in 2010 and it looks like nothing has changed there.

In Firefox however, an addon can actually do this.
Assuming you know how to attach a script to browser.xul, I'm leaving out install.rdf, chrome.manifest and overlay.xul, so here's only the relevant JavaScript:

(function()
{
    // Wait for the browser to settle
    top.addEventListener('load', function load(event)
    {
        // It only needs to do that once
        top.removeEventListener('load', load);
        // Get notified about every page that loads
        top.gBrowser.addEventListener('DOMContentLoaded', function(event)
        {
            // Get the current tab
            var tab = top.gBrowser.mCurrentTab;
            // Check if we already annoyified it
            if(tab.annoyingOrange === undefined)
            {
                // If not, let's do that!
                tab.annoyingOrange = 'Plumpkin';
                // Add a mouseover event to it
                top.gBrowser.mCurrentTab.addEventListener('mouseover', function(ev)
                {
                    // Since we do that to all tabs, we need to check here if we're still the selected tab
                    if(ev.target == top.gBrowser.mCurrentTab)
                    {
                        // And now we can get onto everybody's nerves!
                        alert('Hey apple!!!');
                    }
                });
            }
        });
    });
})();

Tested with Firefox 37.0.1 on Windows.

[ Download .xpi ] (Protip: Unzip for source)

But if your browser does not support it, you are out of luck and there is nothing you can do!


Anyway, this is a very bad thing to do and it annoys people to no end!
This should never, never ever be done in a production or even beta environment!

Community
  • 1
  • 1
Siguza
  • 21,155
  • 6
  • 52
  • 89
1

This works with modern jquery versions 1.8.1 + Popup will show when user clicks above the current window as if they were going to switch tabs or perform some other action. It only pops once so it's less intrusive and it's all customizable message wise and style.

 html
 <div id='confirm'>
 <div class='message'></div>
 <button class='yes'>OK</button>
 </div>

 css
 #confirm {display:none; top:50%; left:50%; transform:translate(-50%, -50%); background:#595959; position:fixed; width:650px; height:auto; padding:40px; text-align:center; box-shadow:0px 0px 22px -2px rgba(0,0,0,0.5); z-index:1000;}
 #confirm button {width:100%; height:70px; color:#fff; background:#595959; display:inline-block; border:1px solid #fff; text-align:center; font-size:12pt; letter-spacing:2px; margin-top:40px; bottom:10px; cursor:pointer; opacity:0.5; transition:0.9s;}
 #confirm button:hover {opacity:1;}
 #confirm .message {color:#fff;}

 js
 var mouseX = 0;
 var mouseY = 0;
 var popupCounter = 0;
 document.addEventListener("mousemove", function(e) {
 mouseX = e.clientX;
 mouseY = e.clientY;
 document.getElementById("coordinates").innerHTML = "<br />X: " + e.clientX + "px<br />Y: " + e.clientY + "px";
 });
 $(document).mouseleave(function (msg, myYes) {
 if (mouseY < 100) {
 if (popupCounter < 1) {
 msg = "There's something in your basket, are you sure you want to leave?";
 var confirmBox = $("#confirm");
 confirmBox.find(".message").text(msg);
 confirmBox.find(".yes").unbind().click(function() {
 confirmBox.hide();
 });
 confirmBox.find(".yes").click(myYes);
 confirmBox.show();
 }
 popupCounter ++;
 }
 });

https://jsfiddle.net/pgx7c5d4/

SJacks
  • 408
  • 3
  • 19
  • In my opinion, this works better than onbeforeunload, which will not fire every time. It's also rather tricky to make exceptions to onbeforeunload. I did add a 'if (window.name != 1)' check, and set the window.name = 1 on the pop-ups first firing. The pages navigating to the page sets the window.name = 0. This make the popup fire only once as long as the user is on the page. Trying to strike a balance between stopping the users from closing the tab and screwing stuff up, and annoying the crap out of the users. Again, spent a whole day looking at my options here, and this one seems to just work – user1544428 Jun 06 '23 at 15:36
-1

Use MouseEvent.clientX and MouseEvent.clientY to track where the mouse is on the document, and then put the popup there using absolute positioning:

//The popup:
var span = document.createElement("span");
span.style.position = "absolute";
span.textContent = "I'm a popup!";
//When the page loads, the popup will be in the top-left corner
//because we can't know where the mouse is on the page load.
document.body.insertBefore(span, document.body.firstChild);

//The event:
document.addEventListener("mousemove", function(e) {
    //Position the span according to its dimensions and where the mouse is.
    span.style.marginLeft = e.clientX-span.clientWidth/2+"px";
    span.style.marginTop = e.clientY-span.clientHeight/2+"px";
});
Noble Mushtak
  • 1,784
  • 10
  • 22
  • I want it to pop up when the mouse is on/inside/over the browser tab – Vasil Hirstoff Apr 20 '15 at 13:14
  • 1
    OK, I fixed my code to accommodate that situation. Doing that is actually much easier than tracking where the mouse left the browser because you don't need to track which side it left at. – Noble Mushtak Apr 20 '15 at 13:22