Jump to content

Help With Open & Closing Browser Windows


jhollin1138
 Share

Recommended Posts

I am using these two JavaScript function to Open and Close a Browser Window.

 

>var morewin=null;
function popWin(linkName) {
if(morewin != null) morewin.close();

morewin = window.open(linkName, "moreWindow", "width=755,height=600,menubar=no,resizable,toolbar=no,scrollbars");
setTimeout("morewin.focus();",200);
}

function closepopWin() {
if(morewin != null) morewin.close();
}

The "popWin" function works fine as written. If the window is open, it will close the window and reopen the window with the new information selected. However, when the "closepopWin" function is selected, it doesn't work. I have even tried doing it this way, and it still doesn't work.

 

>function closepopWin() {
if(false == morewin.closed) morewin.close();
}

I assume I am missing something since I am just learning JavaScript. Can anyone offer any suggestions?

Link to comment
Share on other sites

I created a test page with the code you listed at the beginning of your post, and a couple of links with an onclick event to open and close the popup browser window. Your code appears to be fine. However, I initially had the same issue you did - the open window code worked, but the close window code did not. As it turned out, I had misspelled 'onclick' in the close window link ('onlick'), which prevented the close window code from being run. Once I corrected that, the close window code worked correctly.

 

I'd suggest looking at how you're calling the javascript closepopWin() function in the body of your page, and make sure there aren't any errors / typos in it.

Link to comment
Share on other sites

Thanks for taking a look. I can't seem to find any problems with my code. Here is a sample link I am using to run "popWin".

><a href="popWin('./index.php?action=open&id=579')">
 <img src="./images/open.png" alt="O" name="open579" id="open579" style="border:0;width:15px;height:15px" />
</a>

And here is the code I have that runs "closepopWin".

><a onclick="closepopWin();" href="?action=logout">Logout</a>

I know the "closepopWin" code is working because if I change the code for "closepopWin" to:

>function closepopWin() {
if(morewin != null) morewin.close();
else alert('Nope, did not work!');
}

the "alert" box pops-up.

 

I thought maybe it was the fact I was using both the "href" and the "onclick". So I even tried to use this code (and only use the "href"), and it still didn't work.

>function closepopWin() {
if(morewin != null) morewin.close();
window.location.href = "?action=logout";
}

 

I’m sorry I can’t provide a link to the page because it is a project that I am working and it contains some information I cannot disclose publicly.

 

I know I am missing something; I just can't seem to find it.

Link to comment
Share on other sites

I think you mistyped this part of your code, as it is invalid and doesn't work on my test page:

><a href="popWin('./index.php?action=open&id=579')">

('href' should be 'onclick'...and I don't know what you have for 'href')

 

I know the "closepopWin" code is working because if I change the code for "closepopWin" to:

>function closepopWin() {
if(morewin != null) morewin.close();
else alert('Nope, did not work!');
}

the "alert" box pops-up.

This tells me that you're losing the value of the morewin variable after the popup window has been opened.

 

I don't know what you have for the href attribute in the link that opens the popup window, but if it links to the same page the script is on, or to another page with a copy of this script code on it, the closepopWin() code won't work. Javascript variables and their values do not carry over from one page to another (including a reload of the same page). If the link that runs popLink() also loads a page in the main window, the value of morewin will be lost.

 

I'd suggest using basically a null href with the onclick code to call popLink():

><a href="#" onclick="popWin('./index.php?action=open&id=579')">

If you'd like the link to work in browsers that don't have javascript, or don't have it enabled, you could have the same link in both the href and onlick attributes, but the onclick code must have a 'return false;' at the end to disable the normal link function if the javascript code runs:

><a href="./index.php?action=open&id=579" onclick="popWin('./index.php?action=open&id=579'); return false;">

Link to comment
Share on other sites

...Javascript variables and their values do not carry over from one page to another (including a reload of the same page). If the link that runs popLink() also loads a page in the main window, the value of morewin will be lost.

That is what my problem is. :lol:

 

The links that run "popWin" are in an "iframe". The link that will fire the "closepopWin" is on the parent page of the 'iframe". So that means that the parent page isn't seeing the variable set by the "iframe's" pop-up window. That would explain why the pop-up window will close and reopen with the new data when another link is selected in the "iframe". I have tested it by putting a link to "closepopWin" in the "iframe" and sure enough it worked.

 

Anyway around this problem?

Link to comment
Share on other sites

:lol: I got it working using the following code for "popWin" and "closepopWin".

>parent.frames["searchWin"].morewin=null;
function popWin(linkName) {
if(parent.frames["searchWin"].morewin != null) parent.frames["searchWin"].morewin.close();

parent.frames["searchWin"].morewin = window.open(linkName, "moreWindow", "width=755,height=600,menubar=no,resizable,toolbar=no,scrollbars");
setTimeout("morewin.focus();",200);
}

function closepopWin() {
if(parent.frames["searchWin"].morewin != null) parent.frames["searchWin"].morewin.close();
}

Where the name of the "iframe" I have is set to "searchWin".

 

Thanks for the help! :excl:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...