Link to home
Start Free TrialLog in
Avatar of debitto
debittoFlag for Japan

asked on

JavaScript multiple window.open gradually crashes IE7

My site has a page containing multiple JavaScript scripts to open sub-windows. There is no problem with Firefox or with IE6, but in IE7 I find that any one new window will open properly, and sometimes more than one will; but eventually, after opening and closing windows, I get a blank white one at best. If I close that one and try another, only the border of the window appears. If I then try refreshing the main window, I find it nearly frozen.

I've tried resetting plugins in IE7 and turning off the phishing-detection feature, with no success. I've also searched the Internet for solutions but couldn't any mention of this particular problem. Since it's IE7-specific, and IE is usually less strict than Firefox, I suspect the problem is not with my code. However, I'll attach a sample of the scripts in the head of my document and the links in the body.

Thanks for any help.
<head>
<script language="JavaScript">
function test01()
{
test01open = window.open("../tests/test01.php","test01open"
,"location=0,scrollbars=0,menubar=0,toolbar=0,status=0,resizable=0,directories=0,left=180,top=60,width=580,height=480");
test01open.moveTo(180,60);
}
</script>
</head>
 
<body>
<a href="javascript:void(0)" onClick="test01();return false" onMouseOver="javascript: window.status=''; return true;"onmouseout="javascript: window.status=''; return true;">To take the test, click here</a>
</body>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of debitto

ASKER

Thanks for your reply. Before trying out what you've suggested, I'll confirm that the link is messed up in my original snippet. Don't know why, since I pasted it in.  What I really have is pretty much what you imagined, except that window.status is empty for onMouseOver:

<a href="javascript:void(0)" onClick="test01();return false" onMouseOver="javascript: window.status=''; return true;"onmouseout="javascript: window.status=''; return true;">To take the test, click here</a>

Hope it didn't get garbled again. I'll also add that the trouble occurs when rapidly opening and closing popups (which doesn't faze IE6 or Firefox 2). I've done it more slowly without trouble, but haven't tested exhaustively. Maybe it's just MS bloat. I've reset plugins a couple of times without seeing any benefit.

By the way, congratulations on your Expert Points. You're contributing a lot of help, I see.
:) Thanks
Avatar of debitto

ASKER

Great! Your code works beautifully as-is for a single link, and now that I've made modified copies for my other links, I have not only a solution to my problem but other benefits as well. I'll summarize my understanding of it and my modifications for other people's reference. Please correct me where I go wrong.

1) As long as I don't mind having all popups open in the same place and with the same dimensions, I need only one instance of the code that defines the variable myWindows and creates the function testOpen (inside a script tag, of course). Since I do need other sizes of popup, I've made copies of the function (testOpen01, testOpen02, etc.) with appropriate width and height values.

2) The links must have unique target values, which can be anything, besides pointing to different files. With testOpen modified as testOpen01, etc., everything works. I've done enough testing with IE7 to convince me the problem is gone. My old scripts have become entirely unnecessary; in other words, the resemblance between your target="test01" and my function test01() is coincidental.

Besides working, your code will indeed be convenient to reuse. What exactly did you mean about its being safer?

I'm increasing the points for this, not only because it would have been very difficult for me to arrive at the solution on my own -- and, believe me, I do quite a bit of studying when my real job permits -- but also because it will improve the quality and efficiency of my coding hereafter.

Many thanks.
Avatar of debitto

ASKER

I added this comment when accepting the solution, but that must have been for your eyes only. Here it is for general reference.

Great! Your code works beautifully as-is for a single link, and now that I've made modified copies for my other links, I have not only a solution to my problem but other benefits as well. I'll summarize my understanding of it and my modifications for other people's reference. Please correct me where I go wrong.

1) As long as I don't mind having all popups open in the same place and with the same dimensions, I need only one instance of the code that defines the variable myWindows and creates the function testOpen (inside a script tag, of course). Since I do need other sizes of popup, I've made copies of the function (testOpen01, testOpen02, etc.) with appropriate width and height values.

2) The links must have unique target values, which can be anything, besides pointing to different files. With testOpen modified as testOpen01, etc., everything works. I've done enough testing with IE7 to convince me the problem is gone. My old scripts have become entirely unnecessary; in other words, the resemblance between your target="test01" and my function test01() is coincidental.

Besides working, your code will indeed be convenient to reuse. What exactly did you mean about its being safer?

I'm increasing the points for this, not only because it would have been very difficult for me to arrive at the solution on my own -- and, believe me, I do quite a bit of studying when my real job permits -- but also because it will improve the quality and efficiency of my coding hereafter.

Many thanks.
Thanks - however I would not copy the code just because it needs different dimensions
I would pass the dimensions as parameters


The script can handle a popup blocker by allowing the href to work in case the window.open did not

I removed the moveTo since you have top and left anyway

// create a global var in case you want to close all windows from one place
var myWindows = new Array();
 
// pass the url (mandatory), target (optional), width and height - top and left (optional)
function testOpen(url,target,width,height,top,left) {
  target = (target)?target:"_blank"; // if no target passed, default to blank
  width = (width)?width:"580";
  height = (height)?height:"480";
  top = (top)?top:"60";
  left = (left)?left:"180";
  myWindows[target]=window.open(url,target,
  "left="+left+
  ",top="+top+
  ",width="+width+
  ",height="+height);
  if (myWindows[target]) {
    return false; // cancel the href
  }
  return true; // use the href itself
}
 
and have
<a href="../tests/test01.php" target="test01"
onClick="return testOpen(this.href,this.target)">To take the test, click here</a>
<a href="../tests/test02.php" target="test02"
onClick="return testOpen(this.href,this.target,600,800,0,0)">To take the test, click here</a>

<a href="../tests/test03.php"
onClick="return testOpen(this.href)">All default here</a>
Avatar of debitto

ASKER

Thanks for adding so much help in open discussion. This will save me a lot of trouble and space. I also understand the element of safety as safety from cross-site scripting attack, in view of the way the target is passed to window.open.

Best wishes.