1

I write code to open a popup in new window. I open this window for few seconds after that it will close automatically.What I want is if somebody close it before that limit of time. I will detect it and show him message. Here is code I am using

$(document).ready(function() {

    var myWindow;
    $("#idview").click(function() {
        var vidurl = $('#vurl').val();
        counter();
        myWindow = window.open(vidurl, "popupWindow", "width=600, height=400, scrollbars=yes");

    });

    function counter() {
        var n = $('.c').attr('id');
        var c = n;
        $('.c').text(c);
        setInterval(function() {
            c++;
            if (c <= 41) {
                $('.c').text(c);
            }
            if (c == 41) {
                $('.c').text(n);
            }
        }, 1000);
    }



    setInterval(function() {
        myWindow.close();
    }, 45000);

    window.onbeforeunload = closingCode;
function closingCode(){
   alert('hitme');
   return null;
}


});

I try to use window.ununload but it is not working. can anybody please tell me how to get if somebody is going to close the browser popup?

Thanks

Azad Chouhan
  • 270
  • 1
  • 5
  • 20
  • you can use `onbeforeunload ` to check if anyone closing the popup – RAUSHAN KUMAR Jul 28 '17 at 05:03
  • you can check that is also used in code @RAUSHANKUMAR not working – Azad Chouhan Jul 28 '17 at 05:04
  • You need to bind the onbeforeunload event to the window AFTER it is opened. so move the code to inside the click event . take a look on my answer @azadchouhan – JYoThI Jul 28 '17 at 05:28
  • @azadchouhan the pop-up window url is in the same domain as the parent page ? – JYoThI Jul 28 '17 at 05:58
  • check this http://chandigarhpropertyonline.com/Shareit/Youtubeviews here is my web page on which I am working viewyoutube.js is is my js file – Azad Chouhan Jul 28 '17 at 05:59
  • your opening youtube url that's not the same domain as the parent page . so The best you could do is open a document in your domain that then loads the remote URL in an iframe, or reads it in via server scripts and renders it from there. – JYoThI Jul 28 '17 at 06:12
  • yes I want it to be open in new window not under the parent window – Azad Chouhan Jul 28 '17 at 06:18

3 Answers3

2

You can use onbeforeunload like this :

 window.onbeforeunload = function (e) {
  var e = e || window.event;

};

More reference : https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload

Even possible duplicate of : javascript detect browser close tab/close browser

HEGDE
  • 511
  • 5
  • 17
1

1st : You need to bind the onbeforeunload event to the window AFTER it is opened.

2nd : so move the onbeforeunload event code to inside the click event function

3rd : Window.onbeforeunload change to myWindow.onbeforeunload

    var myWindow;
    $("#idview").click(function() {
        //var vidurl = $('#vurl').val();
        var vidurl = "google.com";
        counter();
        myWindow = window.open(vidurl, "popupWindow", "width=600, height=400, scrollbars=yes");

         myWindow.onbeforeunload = function closingCode(){
                               alert('hitme');
                               return null;
                            }

    });
JYoThI
  • 11,977
  • 1
  • 11
  • 26
0

Here you go with a solution https://jsfiddle.net/rs01x0oy/

var myWindow;
$("#idview").click(function() {
    var vidurl = $('#vurl').val();
    counter();
    myWindow = window.open(vidurl, "popupWindow", "width=600, height=400, scrollbars=yes");
    myWindow.onbeforeunload = function(){
      console.log("Closed");
    };
});

function counter() {
    var n = $('.c').attr('id');
    var c = n;
    $('.c').text(c);
    setInterval(function() {
        c++;
        if (c <= 41) {
            $('.c').text(c);
        }
        if (c == 41) {
            $('.c').text(n);
        }
    }, 1000);
}



setInterval(function() {
    myWindow.close();
}, 45000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="idview" type="submit">
Submit
</button>
Shiladitya
  • 12,003
  • 15
  • 25
  • 38
  • yes its working in the fiddle but not working in my web page.. i donot know why its working – Azad Chouhan Jul 28 '17 at 05:39
  • I need the same thing you did here. even I am not getting any error in console too.. – Azad Chouhan Jul 28 '17 at 05:40
  • Please share your HTML & entire JS code.... This is one more version https://jsfiddle.net/rs01x0oy/1/ ... Update the setInterval. – Shiladitya Jul 28 '17 at 05:50
  • the js code is all I paste above..and for html I use View in my html template – Azad Chouhan Jul 28 '17 at 05:52
  • I tested in my local, it working perfectly. Check the order of JS files... jQuery library file should be included first then your own JS file... Don't forget to include $(document).ready(function(){}); in your JS code... – Shiladitya Jul 28 '17 at 05:56