Ok so I have a site that I want to automatically close the window when the focus is lost from the page. For example, if the user clicks off the page to something else on there desktop I want the page to close. So far I have managed to do this but the grids on my page and the scroll bars all trigger the close function when clicked on. Is there anyway to make the scroll bars and drop down boxes not trigger the function?

This is the code I have so far.

<body onfocusout="javascript:window.opener='x';window.close();">

Thanks!

Recommended Answers

All 6 Replies

Member Avatar for rakhi4110

Hi JOUATT,

Well i used this and i was able to close my parent window. May be try this and see if it works.

<script language="text/javascript">
function closeParent()
{
var s=window.opener;
s.close();
}
</script>

Since you were using window.close() i think it will close your child window, though i'm not sure about that.
See if the above can be of any help to you.

Thanks and regards,
RAKHI

I came up with an error on the function "undefined is null or not an object". The error was triggered when I click on a scroll bar so im thinking that that function would do the same thing that my function would do. Should have mentioned before that I am testing in IE.

Member Avatar for rakhi4110

Though am not exactly sure about your problem, i think i can give a solution if i take a look at the code snippet of your script..

here is a snippet of my code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Alert Window</title>
<script type="text/javascript">
function closeParent()
   
      {
   
      var s=window.opener;
   
      s.close();
   
      }
   
      

    </script>
</head>
<body onfocusout="closeParent();">
<!--<body onfocusout="javascript:window.opener='x';window.close();">-->

blah blah..The rest of my page

I still haven't gotten this to work right. Any help would be appreciative. Thanks.

Member Avatar for rakhi4110

Well JOUATT,

Sry for replying late..
You can use the onBlur event for this.

I'll show a really simple example which worked on IE7,8 and IE9 too..
You can use it if u want in your code and check whether it is working or not..
And kindly inform me if it does...

The code for the parent window..

<html><head>
<script language="javaScript">
function new_child()
{
window.open("x.html","child",toolbar=0,status=0,menubar=0);
}
</script>
</head>
<body>
<form name="f1">
<input type="button" name="b1" onclick="new_child()"/>
</form>
</body>
</html>

And this is the code for the child window x.html

<html>
<head>
<script language="javaScript">
function closeWindow()
{
opener.window.close();
}
</script>
</head>
<body onBlur="closeWindow()">
Hai
</body>
</html>

Inform me in any case. If you want to know how to avoid the confirmation box before the parent window closes,Kindly visit this link
Thanks and regards,
RAKHI

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.