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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

window not defined error 2

Status
Not open for further replies.

peterswan

Programmer
Sep 25, 2002
263
US
Hello,

When I run this script:

<script language="Javascript">
function openWin() {
var iptWin = window.open('somePage.html', 'newWindow', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=375,height=325');

setTimeout('focusWin()',250);
}
function focusWin() {
iptWin.focus();
}
</script>

I get the following error:

iptWin not defined

It draws the error when I try to set the focus within the setTimeout function. When I set the focus without the setTimeout function, there's no error. I need to use the setTimeout function because without it, sometimes the new window loads behind the parent window.

Should I be putting newWindow.focus()?

Why is my window undefined?

Thanks,

Peter [smile]
 
I need to use the setTimeout function because without it, sometimes the new window loads behind the parent window.

When are you calling the function openWin()?

Should I be putting newWindow.focus()?

Using the window.open statement should automatically be setting focus to the newly opened window, so you shouldn't need to set focus to it.

<.

 
Here is all of the code, including the link that calls it:

<script language="Javascript">
function openWin() {
var iptWin = window.open('somePage.html', 'newWindow', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=375,height=325');

setTimeout('focusWin()',250);
}
function focusWin() {
iptWin.focus();
}
</script>

<a href="javascript:eek:penWin()">click to open window</a>

For some reason when I open this window, it sometimes appears behind the parent window. Not always, but sometimes. On another forum, someone told me to set a time out of 250 milliseconds, then set focus to the window I just opened. This will ensure that the new window will in fact get focused.

So that's why I'm calling the setTimeout function, and then the focus.

However, with the setTimeout function, I'm getting the window undefined error. Why could this be? I don't get the error without thte setTimeout function.

Thanks,

Peter [smile]
 
Ok I'm back from lunch, here is your problem:

Code:
<script language="Javascript">
function openWin() {
var iptWin = window.open('somePage.html', 'newWindow', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=375,height=325');

setTimeout('focusWin()',250);
}
function focusWin() {
    iptWin.focus();
}
</script>

The variable iptWin is a local variable defined in the function openWin(). In focusWin(), iptWin is not defined as anything cause it is out of the scope of the local variable iptWin. So do the following:

Code:
<script type="text/javascript">
[!]var iptWin;[/!] 
function openWin() {
iptWin = window.open('somePage.html', 'newWindow', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=375,height=325');

setTimeout('focusWin()',250);
}
function focusWin() {
    iptWin.focus();
}
</script>


<.

 
Hi MonkSnake,

Thanks for the reply.

Still no luck. I made the changes you
recommended, however, now here is the
error I'm getting:

IPTWin is null or not an object.

Thanks,

Peter [smile]

 
Can you post your Javascript verbatim as you have it now. I'm guessing you still have:

Code:
var iptWin = window.open('somePage.html', 'newWindow', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=375,height=325');

Remove the var at the beginning of it.

I did test my code and it worked.

<.

 
Why don't you try this?
Code:
<script type="text/javascript">
var iptWin; 
function openWin() {
iptWin = window.open('somePage.html', '[b][red]iptWin[/red][/b]', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=375,height=325');

iptWin.onload = 'iptWin.focus()';
}

</script>

Lee
 
I will post the script that I have that works:

Code:
<script type="text/javascript">
var iptWin;
function openWin() {
   iptWin = window.open('[URL unfurl="true"]http://www.google.com',[/URL] 'newWindow', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=375,height=325');
   setTimeout('focusWin()',250);
}

function focusWin() {
    iptWin.focus();
}
</script>

<.
 
<script type="text/javascript">
var iptWin;
function openWin() {
iptWin = window.open('somePage.html', 'iptWin', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=375,height=325');
setTimeout('focusWin()',250);
}

function focusWin() {
iptWin.focus();
}
</script>

<a href="javascript:eek:penWin()">click to open window</a>
 
Thanks to both MonkSnake and Trollicious,

I used MonkSnakes final version and it works fine. I'm still not sure what was wrong with the code. I thought that my previous version was identical to Monksnake's version, but I must have been wrong.

The code that works is posted above. Hopefully this will keep the window in the front.

Thanks again,

Peter [smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top