Forum Moderators: open

Message Too Old, No Replies

window.open() fails to open a new window

         

CMaso

6:45 pm on Jun 10, 2009 (gmt 0)

10+ Year Member



Using this function to open new windows:

function fncSpawnWindow(n_strUrl) {
var strOptions = 'width=620,height=400,directories=no,location=no,menubar=yes,personalbar=no,' + 'resizable=yes,status=yes,toolbar=yes,scrollbars=yes';
var objWin = window.open(n_strUrl, '', strOptions);
objWin.focus();

return (objWin);
}

Works almost of the time, but on certain testers' PCs a js error pops up - "'objWin' is null or not an object". Error seems to occur at objWin.focus() call - because objWin isn't an object. So I comment out this line, and the return line after it, and then there's no js error, but nothing at all happens; no new window pops open! This especially seems to happen when trying to call window.open() from a window that was itself created by window.open(). Has anyone else experienced something like this? Using IE 7.

LifeinAsia

6:52 pm on Jun 10, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



What if you name the window?

var objWin = window.open(n_strUrl, 'somename', strOptions);

CMaso

7:29 pm on Jun 10, 2009 (gmt 0)

10+ Year Member



I think I tried that a while ago with no difference, but it wouldn't hurt to name the window, deploy the change, and see if the tester continues to get the error. In any case, there are plenty of places where unnamed window objects are getting created without any problems.

LifeinAsia

9:16 pm on Jun 10, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



But since you mentioned that it especially happens when trying to open a new window from a window that was itself spawned from another window (presumably also as an unnamed window), that could be a big part of the problem. One unnamed window? Probably okay. The second one? I'm guessing not so okay.

rocknbil

5:23 pm on Jun 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard cMaso.

First, why do you need to return the window object from this function? The reason I ask is that a normal call to a new window is something like this:

<p><a href="help.html" onClick="return fncSpawnWindow('help.html');">help</a></p>

If javascript is disabled, the link to help.html will still work, making your content accessible to all. If javascript is enabled, return false tells the browser to ignore the link and allow the function to handle the click. So what you generally want ending a window.open function is return false.

Second, the "window name" parameter (and others) are pretty misunderstood. Window name is an ID assigned to a window. If you just assign "windowName" to all opening windows, and the user returns to the main window leaving the pop up open, any subsequent links to this function WILL load in the already-open window.

But they will load behind the opener, and the user thinks your site is broken.

I agree with LIA, a "blank" value for "window name" is a good suspect for "null or not an object." Note my changes below, using the date() function, which insures a new window will always be opened for any clicked link (if it's not blocked.)

because objWin isn't an object.

if you were to put

alert(objWin);

just before objWin.focus();, you would get an alert that says "[object Window]" - objWin is indeed an object.

Last, two bits of advice:

- You don't need to use "no","yes","1", or "0" for window parameters. just include them if you want them, leave them off if you don't.
- Never ever leave off resizable or scrollbars (which I see you've included, so it's all good).


<script type="text/javascript">
function fncSpawnWindow(n_strUrl) {
var day = new Date();
var id=day.getTime();
var strOptions = 'width=620,height=400,menubar,resizable,status,toolbar,scrollbars';
var objWin = window.open(n_strUrl, id, strOptions);
objWin.focus();
return false;
}
</script>
<p><a href="help.html" onClick="return fncSpawnWindow('help.html');">help</a></p>