Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

closing a Window if it exists...else do nothing

Status
Not open for further replies.

Sheffield

Programmer
Jun 1, 2001
180
US
Please help me before I kill myself.

My goal is this: Close a browser window if it already exists, else do nothing.

When I open a new window using the second function below, the first function will correctly close it. However, if 'openMyWindow' is not called, 'closeMyWindow' raises an error that 'MyWindow' is undefined. If I declare a variable called MyWindow, 'closeMyWindow' doesn't fire correctly.

Any suggestions?


function closeMyWindow() {
if (MyWindow && !MyWindow.closed) {
MyWindow.close();
}
}

function openMyWindow(Page,W,H){
var options = "width="+W+",height="+H;
MyWindow = window.open(Page,'PageWin', options);
MyWindow.focus();
}
 
Do something like this:

MyWindow=null;
function closeMyWindow() {
if (MyWindow) {
MyWindow.close();
}
}

function openMyWindow(Page,W,H){
var options = "width="+W+",height="+H;
MyWindow = window.open(Page,'PageWin', options);
MyWindow.focus();
}

Then it should work. Let me know if you have any more problems...
 
Sheffield,

Although I would never want to stand in the way of a person who wanted to kill themselves, if your only reason for doing so is a result of frustration at the action of a JavaScript window, then allow me to reassure you.

This may help. It is an HTML file. When you click the "open window" button, it opens a window which disappears after a few seconds. While the window is open, you can click the second button to dismiss it. However, after the timeout, if the second window goes away, clicking the dismiss button has no effect.

Feel free to munge the code to fit your needs:

save this as "Sample.html":

Code:
<!DOCTYPE html 
     PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
     &quot;[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>[/URL]

<html xmlns=&quot;[URL unfurl="true"]http://www.w3.org/1999/xhtml&quot;[/URL] xml:lang=&quot;en&quot; lang=&quot;en&quot;>
  <head>
    <meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;></meta>
    <title>JavaScript Sample</title>
    <script src=&quot;Sample.js&quot; type=&quot;text/javascript&quot;></script>
  </head>
  <body>
    <input type=&quot;button&quot; value=&quot;Press me to pop open the warning window.&quot; onclick=&quot;Warning('open');return false;&quot;></input>
    <br /><br />
    <input type=&quot;button&quot; value=&quot;Press me to close the warning window.&quot; onclick=&quot;Warning('close');return false;&quot;></input>
  </body>
</html>

save this as &quot;Warning.html&quot;

Code:
<!DOCTYPE html 
     PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
     &quot;[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>[/URL]

<html xmlns=&quot;[URL unfurl="true"]http://www.w3.org/1999/xhtml&quot;[/URL] xml:lang=&quot;en&quot; lang=&quot;en&quot;>
  <head>
    <meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;></meta>
    <title>Warning</title>
  </head>
  <body>
    <p><b>Note:</b> This process might take a few minutes to run...</p>
  </body>
</html>

save this as &quot;sample.js&quot;

Code:
function Warning(Argument)
  {
    if(Argument==&quot;open&quot;)
      {
        Win_Warning = window.open('Warning.html','Win_Warning','top=50,screenY=50,left=50,width=500,height=100,scrollbars=no,scrollbar=no,menubar=no');
        setTimeout(&quot;Warning('close')&quot;,5000)
      }

    if(Argument==&quot;close&quot;)
      {
        if (Win_Warning.closed+&quot;&quot; == &quot;false&quot;)
          {
            Win_Warning.close();
          }
        else
          {
          }
      }
  }

Now, double-click Sample.html...

Hope that helps!

Cheers,
[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top