0

When I try to close the page or tab I want to open a popup with no option to close it.

I am using this script but it is not working.

<script language="JavaScript">
    window.onbeforeunload = closepopup;
    function closepopup(){
        var id = '#dialog';

        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set heigth and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});

        //transition effect     
        $('#mask').fadeIn(500); 
        $('#mask').fadeTo("slow",0.9);  

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        $(id).css('top',  winH/6-$(id).height()/6);
        $(id).css('left', winW/2-$(id).width()/2);

        //transition effect
        $(id).fadeIn(2000);     

    }
</script> 
bytecode77
  • 14,163
  • 30
  • 110
  • 141
  • How is it not working? Be more specific! – Stibu Sep 26 '15 at 15:48
  • Along with specifically how it is not working, the HTML snippets for #dialog and your HTML (simplified) would be helpful. – Romuloux Sep 26 '15 at 15:54
  • Please check this URL http://avirasofttech.com/demo/ . You can check the entire code in the source code. When you try to close the page the browser default popup appears. And when you click on stay on the page. I will show my popup. But I don't want to show the browser default popup. It should directly show my popup. I know my explanation is not good. But you will understand when you check out the URL. Thank you – Vikranc3119 Sep 26 '15 at 20:11

1 Answers1

0

It's not possible to prevent someone from closing the browser any more (thankfully), but have a look here: how to block users from closing a window in javascript?

You need to return false from your function, and the broswer will pop up asking if the user intends to leave. You can't customise this message, but you could display your custom pop up behind it. The following code for instance turns the background red before displaying the browser's standard 'do you want to leave' prompt.

window.onbeforeunload = closepopup;
function closepopup(e){
    document.bgColor="red";
    return false;
}
Community
  • 1
  • 1
enigma
  • 3,476
  • 2
  • 17
  • 30
  • I have already tried that. The problem is. When I try to close the window. It pop us a massage. "Are you sure you want to leave this page? and two buttons"Leave this page" and "Stay on the page". If I stay on the page then my POPUP shows us. I want to show my popup directly. – Vikranc3119 Sep 26 '15 at 19:35
  • @Vikas yeah, that's the browser's default, and AFAIK you can't change it. Not allowing a user to close a browser page is a step that the browsers have understandably tried to mitigate. – enigma Sep 26 '15 at 19:37