• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

close popup at page unload

how do i close a popup when the user leaves that page? i am using the popup function below:
<script language = "javascript">
function popup()
{
var win
win = window.open("HelpFile/alert.htm", "Help", "width = 500, height = 300, top = 50, left = 450, scrollbars = no, resizable = no");
}
</script>
0
coletteck8
Asked:
coletteck8
  • 4
  • 3
  • 3
2 Solutions
 
ZylochCommented:
Hi coletteck8,

Try this:
<body onunload="if (win&&!win.closed){win.close();}">

Regards,
Zyloch
0
 
cLFlaVACommented:

Something like this in your body tag:

<body onunload="if (win && !win.closed) { win.close(); }">
0
 
cLFlaVACommented:

Apologies Zyloch.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
coletteck8Author Commented:
i'm getting an error win undefined
0
 
coletteck8Author Commented:
this is what i have
<script language = "javascript">
function popup()
{
var win;
win = window.open("HelpFile/alert.htm", "Help", "width = 500, height = 300, top = 50, left = 450, scrollbars = no, resizable = no");
}
</script>
<body onunload = "if (win &&!win.closed){win.close();}">
0
 
cLFlaVACommented:
Declare var win outside of the function.

<script language = "javascript">
var win;
function popup()
{
win = window.open("HelpFile/alert.htm", "Help", "width = 500, height = 300, top = 50, left = 450, scrollbars = no, resizable = no");
}
</script>
<body onunload = "if (win &&!win.closed){win.close();}">
0
 
ZylochCommented:
No prob, cLFlaVa, I'm just experiencing an onrush of questions after two weeks of very difficult school lol

Hmm... It should work. Try this:

if (Help && !Help.closed) {Help.close();}

But before should work. Make sure that your popup opens before you close.
0
 
ZylochCommented:
Ah, so now it's my turn cL :)
0
 
cLFlaVACommented:
hehe
0
 
coletteck8Author Commented:
OF course. i'm losing my mind here! thanks so much to both of you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now