Forum Moderators: open
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.
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>