Archive

Posts Tagged ‘Javascript’

Do you want to close this window?

May 15, 2009 1 comment

Sometimes you would want to close the window programatically using javascript:
window.close();.

There is a joke, I recall now, about Microsoft. A guy used a car and that car’s software was controlled by Microsoft Windows CR. One day that guy got frustrated at his work because of some stupid bug and was outrageously driving his car at top speed. Eventually his car became uncontrolable and he was about to bump in to the truck that was going before him. His cars air-bag was supposed to open now. Now the Microsoft Windows CR asked that guy a very smart question: Do you really want to open the air-bag? BANG!

Now, back to our problem: When you try to close an existing window programatically using javascript windows will behave very smart by asking the user: The web page you are viewing is trying to close the window. Do you want to close this window?

Now functinally,this window is supposed to be closed and user does not want to see this smart question asked by windows OS. What would you do?

Using the below code would not bring up that message:


function closeWithOutconfirm()
{
this.focus();
self.opener = this;
self.close();
}

Catch Window Close Event

May 15, 2009 8 comments

Once in a while people ask me how to catch the IE window’s close event. One thing to use is onbeforeunload event of the window. But the problem in using this event is, you never know when this onbeforeunload event is called. This event could be called for numerous reason like navigation to another page with in the application. So you cannot exactly catch the browser close event using onbeforeunload.

The mouse pointer position comes in handy in this cases. We can effectively harness the power of both onbeforeunload and clientY position to exactly catch the browser close event.

Code:

window.onbeforeunload = onunload;
function onunload()
{
var closeEvent=false;
if (event.clientY < 0)
closeEvent = true;

if ((closeEvent))
{
//Write your code here for close event.
}
}

Is there any other efficient way to do this?

Update:

Anonymous: This so very well works with internet explorer 8; i have also tested the tabs it works well. I have given the full code here. However, obviously, there is a requirement: You must allow the execution of javascript.

<HTML>
<head>
<script>

    window.onbeforeunload = closeit;

    function closeit() {
        var closeEvent = false;
        alert(event.clientY);
        if (event.clientY < 0)
            closeEvent = true;

        if ((closeEvent)) {
            alert("do you want to close the window");
            //your actions during closing comes here
        }
    }

</script>
</head>
<body>
  <a href="https://chillicode.wordpress.com">Click here to navigate to
      Chillicode</a>
</body>
</html>

The above code helps us to catch the window close event and there by we can do what ever we wish to do.

In some cases it is enough to just alert the user that he is trying to close the window; how to achieve it? That is even more simple; just return a string value in the closeit function

function closeit()
{
  return "Any string value here forces a dialog box to \n" + 
         "appear before closing the window.";
}