0

I am working on a piece of code but i am not able to get it to work on mobile because there is no mouseout event .. i am looking to convert this code into something that works on mobile .. if a mobile user goes to another window or tab on his mobile phone from the one containing the div , I wish to hide the div permanently.

How do I achieve similar working code on mobile ?

<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") {
            document.getElementById('ControlCode').style.display = "none";
            document.getElementById('ControlCode').style.visibility = "hidden";
            }
        });
    });
    </script>
Go Fresher
  • 15
  • 1
  • 6

1 Answers1

0

Maybe you can use somethings like this :

$(window).focus(function() {
  //code when user is in your tab
});

$(window).blur(function() {
  //code when user leave your tab
});

From this answer : https://stackoverflow.com/a/1760268/4226012

thib3113
  • 564
  • 7
  • 19
  • This does not work as when i click the div , the div closes – Go Fresher Jul 02 '17 at 09:59
  • when i am on the page , i cannot interact with the content inside div tag – Go Fresher Jul 02 '17 at 10:00
  • Check the event . If the Target elements is your div, don't close it . – thib3113 Jul 02 '17 at 10:06
  • if there is a div with a link to "google.com" and when i click on the div and visit google website , if i come back to the page which has that div , I want the div to close automatically if user moves away from page .. how to achieve it? – Go Fresher Jul 02 '17 at 10:08