Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

Can't open popup window question

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
musicarr
Asked:
musicarr
  • 3
  • 2
1 Solution
 
DennisParaguayCommented:
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
 
musicarrAuthor Commented:
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
 
joeaspCommented:
Use a piece of javascript in the following form:

window.resizeTo(width,height)
0
Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

 
DennisParaguayCommented:
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
 
musicarrAuthor Commented:
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
 
musicarrAuthor Commented:
Thanks again for your help. It's so good to get something to work like you want it to.

Mark
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

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