Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 22nd, 2005, 09:32 AM
Registered User
 
Join Date: Mar 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to msprothero
Default Window status works in FF, but not IE...why?

I have a project where I need to check to see if a pop-up window I've created is still open. I've pieced together some code, but it only works correctly in FF, but not IE. JavaScript is not my forte and I'm at a loss as to what the issue is.

The code below works fine as is, but if I change this line:
      <a href="#" onclick="winOpen('','testSite','295','300','no');r eturn false;">Open</a><br>

to this:

      <a href="#" onclick="winOpen('http://www.msn.com','testSite','295','300','no');return false;">Open</a><br>

it doesn't work in IE...it comes back saying the window doesn't exist even if it does.

Any ideas?

Thanks!
Chris


<html>
   <head>
      <title>Window Test</title>
      <script language="JavaScript" type="text/javascript">
      <!--
         function winOpen(url, myname, w, h, scroll)
         {
            var winl = (screen.width - w) / 2;
            var wint = ((screen.height - h) / 2)-20;

            winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl +',scrollbars='+scroll+',toolbar=0, status=0, resizable=0,menubar=0'
            eval(myname+" = window.open(url, myname, winprops)");
            if (parseInt(navigator.appVersion) >= 4 || parseInt(ie.appVersion) >= 4) { eval(myname+".window.focus()"); }
         }

         function winCheck(winSite)
         {
            try{
               if(eval("window."+winSite+".document"))
               {
                  alert("Window \""+winSite+"\" open");
               }
               else
               {
                  alert("Window \""+winSite+"\" not found");
               }
            }
            catch(e){
               alert("Window \""+winSite+"\" not found");
            }
         }

         function winClose(winSite)
         {
            try{
               if(eval("window.testSite.document"))
               {
                  window.testSite.close()
               }
               else
               {
                  alert("Window \""+winSite+"\" not found");
               }
            }
            catch(e){
               alert("Window \""+winSite+"\" not found");
            }
         }
      //-->
      </script>
   </head>
   <body>
      <h1>Window Test</h1>
      <a href="#" onclick="winOpen('','testSite','600','400','no');r eturn false;">Open</a><br>
      <a href="#" onclick="winCheck('testSite');">Check</a><br>
      <a href="#" onclick="winClose('testSite');">Close</a><br>
   </body>
</html>
 
Old March 22nd, 2005, 09:56 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You need to store the child window in a global variable:
Code:
var oChild = null;
function winOpen(Url)
{
  return window.open(Url); //Add extra parameters if needed
}

function checkWindow(Window)
{
  return (Window && !Window.closed);
}

function closeWindow(Window)
{
  if (checkWindow(Window))
  {
    Window.close();
  }
}
Then in your code:
oChild = winOpen(...);

Later

closeWindow(oChild);

As a side issue NEVER use eval, you don't need it and it's a terrible resource hog. e.g.
eval("window.testSite.document") just needs to be =>
window.testSite.document

--

Joe (Microsoft MVP - XML)
 
Old March 22nd, 2005, 10:10 AM
Registered User
 
Join Date: Mar 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to msprothero
Default

Excellent! Thank you for your very quick response. I'll give this a try. Thanks for the pointers and explanation too! :-)

Chris






Similar Threads
Thread Thread Starter Forum Replies Last Post
Window.status Bar [email protected] C# 2005 6 January 9th, 2008 05:18 AM
Works in IE but not in FF sofiacole Pro JSP 1 September 12th, 2007 11:20 PM
Javascript works in IE7 not in FF 2 swifty_programmer Javascript 2 August 31st, 2007 02:07 AM
hide the status bar of window at runtime yeetesh Pro VB 6 5 January 10th, 2005 02:38 PM
Cross-frame scripting, works in FF but not IE Snib Javascript 4 October 25th, 2004 08:54 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.