How to close opened popup window by clicking a button?

S

Sujan

Hello all,

Help me!

--------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script language="JavaScript">
<!--
function popMe() {
openMe = window.open('http://www.google.com/','google','location=no,
status=no, width=640, height=480, left=100, top=100');
openMe.focus();
}

function closeOpened() {
closeIt = window.close('google'); // I need help on this line.
}
//-->
</script>

</head>

<body>

<input type="button" value="Click me!" onclick="popMe();" /> <input
type="button" value="Close opened" onclick="closeOpened();" />

</body>
</html>
--------------------

I have one button says "Click me!" when I click on that button its
calls a function "popMe()" and opens a popup window. And also I have
another button which says "Close Opened" and when I click on it, it
doesn't close the opened window.

Is my script is wrong? Help me on the function "closeOpened()".

Thank you in adv.
 
D

Daniel Kirsch

Sujan said:
function popMe() {
openMe = window.open('http://www.google.com/','google','location=no,
status=no, width=640, height=480, left=100, top=100');
openMe.focus();
}

function closeOpened() {
closeIt = window.close('google'); // I need help on this line.
}

You must call the close method of your opened window, not from your
current one:

if (openMe && openMe.close && !openMe.closed)
openMe.close();

Daniel
 
S

Sujan

Thanks.

It worked, but there's a problem. When i click on "Close Opened"
without opening a popup, it says error on page 'openMe' is undefined.

Can I alert the user when they click first "Close Opened" button
without opening popup.

I'll be Thankful.
 
M

McKirahan

Sujan said:
Hello all,

Help me!

--------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script language="JavaScript">
<!--
function popMe() {
openMe = window.open('http://www.google.com/','google','location=no,
status=no, width=640, height=480, left=100, top=100');
openMe.focus();
}

function closeOpened() {
closeIt = window.close('google'); // I need help on this line.
}
//-->
</script>

</head>

<body>

<input type="button" value="Click me!" onclick="popMe();" /> <input
type="button" value="Close opened" onclick="closeOpened();" />

</body>
</html>
--------------------

I have one button says "Click me!" when I click on that button its
calls a function "popMe()" and opens a popup window. And also I have
another button which says "Close Opened" and when I click on it, it
doesn't close the opened window.

Is my script is wrong? Help me on the function "closeOpened()".

Thank you in adv.

Will this help? Watch for word-wrap.

<html>
<head>
<title>closer.html</title>
<script type="text/javascript">
var pop;
function popOpen() {
pop = window.open('http://www.google.com/','google','status=no,width=640,
height=480,left=100,top=100');
}
</script>
</head>
<body>
<form>
<input type="button" value="Click me!" onclick="popOpen();" />
<input type="button" value="Close it!" onclick="pop.close()" />
</form>
</body>
</html>


Of course, you can still have:

function popClose() {
pop.close();
}

<input type="button" value="Close it!" onclick="popClose()" />
 
D

Daniel Kirsch

Sujan said:
It worked, but there's a problem. When i click on "Close Opened"
without opening a popup, it says error on page 'openMe' is undefined.

You need to either check the existence of your global variable first or
define one before.

Can I alert the user when they click first "Close Opened" button
without opening popup.

if (typeof openMe == "object" && openMe.close && !openMe.closed)
openMe.close();
else
alert("No window open");

Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top