1

I want to write a javascript such that :: When I bring the mouse over the close window and tab, it should give me an alert at that time only. I have written that script but it is executing whenever I bring the mouse over the "minimize", "restore down", "close", or whenever I hover the mouse over the tab. I want the logic to execute only on first mouse hover over close window and tab. It should not repeat whenever I hover the mouse over the close window/tab.

<style>
  .banner-text>div>.banner-link.link-highlight
  {
     display: none;
  }

.booking-widget-home 
    {
  background-image:url('https://www.chimp.in/content/dam/chimp/website/banner/target/12/upgrade-banner.jpg') !important;
  }

 .banner-title 
 {
color: white;
    }

.banner-description 
{
color: white;
   } 

  .banner-link.link-highlight
  {

    border-radius: 10px;
  }

  .banner-text > p > span 
  {
    bottom: -20px;
    position: relative;
  }

   div.knowMore
{
display: block;
}

</style>
<script>

(function()
{
     'use strict';
      var cnt=6000;
      function init() 
      {
          if(typeof jQuery !="undefined")
          {         
            windowClose();  
           }
          else
          {
              cnt=cnt-500;
              if(cnt>500)setTimeout(init,500);
          }
      }


      var addEvent = function(obj, evt, fn) 
      {
    if (obj.addEventListener) 
    {
        obj.addEventListener(evt, fn, false);
    }
    else if (obj.attachEvent) 
    {
        obj.attachEvent("on" + evt, fn);
    }
    };

addEvent(document, "mouseout", function(event) 
{
    event = event ? event : window.event;
    var from = event.relatedTarget || event.toElement;
    if ( (!from || from.nodeName == "HTML") && event.clientY <= 100 ) 
    {

    alert("Please Browse through website before exit!");
    }
});                                      


function windowClose()
{

    some other logic ....
}
 init();
})()
</script>
Ambika Tewari
  • 251
  • 3
  • 9
  • 1
    Firstly, hook to the `beforeunload` event to do what you need. Secondly, showing an alert to every user telling them to browse the entire site before they leave is incredibly annoying, to the point where it will probably drive people away. – Rory McCrossan Sep 17 '18 at 07:35
  • check this. https://stackoverflow.com/a/3888938/3843967 – Berk Akkerman Sep 17 '18 at 07:35
  • 1
    If i understood your requirement exactly, you need to declare a global variable just after 'use strict' .e.x `var user_alerted = false;` and inside the function addEvent(document) after the alert() set to true `user_alerted=true` , then before the alert("please browse") make a check if its false, if not exit the function like `if(user_alerted)return;` – gijoe Sep 17 '18 at 07:37
  • Hi @RoryMcCrossan that alert is just for example , I am surely not going to make it live to all users. – Ambika Tewari Sep 17 '18 at 07:43
  • Hi @gijoe Thank you .. that worked. Now code is executing only once. But would like to execute only when mouse over is done on close(x) window or tab. but this is executing when i hover the mouse over tab/window. Can you please advise something on that ? – Ambika Tewari Sep 17 '18 at 08:35
  • Please check more info about beforeunload event like @RoryMcCrossan stated , but as firefox docs states browsers ignore alert,confirm boxes and you can only return a message (which also might be ignored, need to test thoroughly). In that case if you are to proceed and use the onbeforeunload event, in order not to show it again to the user, is suggested to use a cookie, where you will save that you used this event and next time when you load the page , if that cookie exists you wont add an eventlistener for the beforeunload event – gijoe Sep 17 '18 at 12:47

0 Answers0