?
Solved

Can't open popup window question

Posted on 2003-03-07
6
Medium Priority
?
230 Views
Last Modified: 2013-12-25
Hi all,

Here's a quick question for you. Below is the code I'm using, but it is not working as I expect it would. What I'm trying to do it open a popup window and then the user can close it with the "close window" link within the popup window. That part is working fine...for the the first time only. After the user closes the window the links on the opener page don't work anymore. I
get a javascript error which says, "win.close is not a function.", each time I try to open a new window.

I also want the script to work so that when, after say window #1 is opened, the user clicks to open window #2, the first window closes and a new one is created, hense the code, "if(window.win) {win.close()}". This part is not working at all.

I think I'm close, but...no cigar. What am I missing here?!!

Thanks in advance for your help.

Mark
____________________________________________

<html>
<head>
<title>Popuptest</title>
<script language="javascript" type="text/javascript">
<!--
function popup(title,text) {
var w = 206;
var h = 150;
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
if(window.win) {win.close()}
win = window.open('','','width='+w+',height='+h+',top='+wint+',left='+winl+',scrollbars=no');

var content = '<html>';
content += '<head><title>'+title+'</title>';
content += '</head>';
content += '<body bgcolor="white">';
content += text+'<br><br>';
content += '<div align="center">';
content += '<a href="javascript:window.close()">Close window</a>';
content += '</div>';
content += '</body></html>';
win.document.open();
win.document.write(content);
win.document.close();
}
//-->
</script>
</head>
<body bgcolor="#ffffff">
<a href="javascript:;" onclick="popup('Popup 1','this popup window #1')">open window #1</a>
<br><br>
<a href="javascript:;" onclick="popup('Popup 2','this popup window #2')">open window #2</a>
</body>
</html>
0
Comment
Question by:musicarr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:DennisParaguay
ID: 8089688
Why should you close a window to open a new one? Use this code:

************************************
<html>
<head>
<title>Popuptest</title>
<script language="javascript" type="text/javascript">
<!--
function popup(title,text) {
var w = 206;
var h = 150;
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
win = window.open('','popupwindow','width='+w+',height='+h+',top='+wint+',left='+winl+',scrollbars=no');
win.focus();
var content = '<html>';
content += '<head><title>'+title+'</title>';
content += '</head>';
content += '<body bgcolor="white">';
content += text+'<br><br>';
content += '<div align="center">';
content += '<a href="javascript:window.close()">Close window</a>';
content += '</div>';
content += '</body></html>';
win.document.open();
win.document.write(content);
win.document.close();
}
//-->
</script>
</head>
<body bgcolor="#ffffff">
<a href="javascript:;" onclick="popup('Popup 1','this popup window #1')">open window #1</a>
<br><br>
<a href="javascript:;" onclick="popup('Popup 2','this popup window #2')">open window #2</a>
</body>
</html>
************************************

(If you specify the name of the window (in this case "popupwindow") there won´t be opened another window with the same name.)

Seems to work fine for me, I jope I could help you!

-Dennis
0
 

Author Comment

by:musicarr
ID: 8092521
Hi Dennis,

Thanks for the answer. This works great, except forgot about one thing, which you had a question on. I was closinging the old window before opening the new one because the new window might be a different size than the old one. I can pass the size I want the new window to be, such as:

<a href="javascript:;" onclick="popup('Popup 1','this popup window #1',320,400)">open window #1</a>

and then adjust the code in the javascript as:

function popup(title,text,width,height) {
var w = width;
var h = height;

But it doesn't seem to "resize" the current window. How do you accomplish that?

Thanks,

Mark
0
 
LVL 1

Expert Comment

by:joeasp
ID: 8096638
Use a piece of javascript in the following form:

window.resizeTo(width,height)
0
Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

 
LVL 2

Accepted Solution

by:
DennisParaguay earned 500 total points
ID: 8098143
Now you can adjust the size and it centers automatically:

************************************
<html>
<head>
<title>Popuptest</title>
<script language="javascript" type="text/javascript">
<!--
function popup(title,text,width,height) {
var left = (screen.width - width) / 2;
var top = (screen.height - height) / 2;
win = window.open('','popupwindow','scrollbars=no');
win.focus();
var content = '<html>';
content += '<head><title>'+title+'</title>';
content += '</head>';
content += '<body bgcolor="white" onLoad="window.resizeTo('+width+','+height+');window.moveTo('+left+','+top+');">';
content += text+'<br><br>';
content += '<div align="center">';
content += '<a href="javascript:window.close()">Close window</a>';
content += '</div>';
content += '</body></html>';
win.document.open();
win.document.write(content);
win.document.close();
}
//-->
</script>
</head>
<body bgcolor="#ffffff">
<a href="javascript:;" onclick="popup('Popup 1','this popup window #1',200,400)">open window #1</a>
<br><br>
<a href="javascript:;" onclick="popup('Popup 2','this popup window #2',400,200)">open window #2</a>
</body>
</html>
************************************

I hope I could help you!

-Dennis
0
 

Author Comment

by:musicarr
ID: 8170227
Sorry for the delay in getting back. This will work great. Thank you so much. This however brings up another question. I've taken what I've learned here and put it into another script but ran across a unique problem. Check out this link. (I put the question in the right section - Javascript - this time.

http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20556683.html

Thanks again for your help.

Mark
0
 

Author Comment

by:musicarr
ID: 8170233
Thanks again for your help. It's so good to get something to work like you want it to.

Mark
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question