Click to See Complete Forum and Search --> : Problem with window.focus
vbasp
February 7th, 2003, 09:08 AM
I have a problem. We know that we can open many windows with names to all windows by the command thru javascript:
window.open('','name1','');
Now I have opened suppose three windows with names win1, win2, win3. Now i want to check whether window win2 is opened or not and if it is opened i want to focus it. Can anyone help me out to do it. I tried as:
if(win2.closed)
window.open('',win2,'');
else
win2.focus();
But it gives the error 'THIS METHOD OR PROPERTY IS NOT AVAILABLE' or something similar only. what is the proper way to do it. thanx in advance.....
antares686
February 10th, 2003, 05:24 PM
Try this
<Script language=javascript>
objwin2 = null; // Global variable for our object reference.
function OpenFocus(){
if (!objwin2){ // If the object variable not set open and set it.
objwin2 = window.open('http://www.rr.com','win2','');
}
else {
if (objwin2.closed) { // If is set but window is closed, reopen.
objwin2 = window.open('http://www.rr.com','win2','');
}
else { // Otherwise set focus to the referenced window.
objwin2.focus();
}
}
}
</script>
<input type=button OnClick="OpenFocus()" value="Push">
This is valid for IE, not sure about Netscape.
vbasp
February 11th, 2003, 02:06 AM
Hey thanx for the reply. But I am not able to do it. I tried the way as:
<Script Language=javascript>
function openWin()
{
var i, newWin;
for(i=1;i<4;i++)
{
newWin = window.open('',i,'');
newWin.document.write('Name is: ' + newWin.name);
}
newWin = null;
openFocus();
}
function openFocus()
{
if(!newWin)
newWin = window.open('','2','');
else
{
if(newWin.closed)
newWin = window.open('','2','');
else
newWin.focus();
}
}
</Script>
<BODY onload=openWin()>
</BODY>
But it is not focusing the focus on window 2. Am I writing the correct code to open the window with name i(1,2,3) or somethign else is to be written.
antares686
February 11th, 2003, 05:37 AM
First off, the newWin item has to be at the global level for it to be seen by all functions and used in them
Next with the fact you are dealing with multiple windows you have to create individual variable objects or a single array object (I opt for array).
Now based on you needs there are some finer points about changing focus that become an issue with setting it immediately. Forutnately most are covered by just waiting a moment then setting.
Based on what you have here is what I think you are after.
<Script Language=javascript>
var oNewWin = new Array(); // oNewWin has to be created here to be reusable by all functions. Array of objects.
var oInterval = ""; // Our interval object for timer.
function openWin()
{
var i;
for(i=0;i<3;i++)
{
oNewWin[i] = null; // This shouldn't be needed but I have always used it myself.
oNewWin[i] = window.open('',i,'');
oNewWin[i].document.write('Name is: ' + oNewWin[i].name);
}
// Immediate focus is not available in some OSs, window will just blink in task bar.
// This handles most of those situations by allowing final window enough time to be
// opened and focus allowed to be removed.
oInterval = window.setInterval("AutoFocus(0)",2000); // Wait 2 seconds and set focus.
}
function AutoFocus(winNum)
{
window.clearInterval(oInterval); // Clear interval timer so does not run again.
openFocus(winNum); // Set our focus.
}
function openFocus(winNum)
{
if(!oNewWin[winNum]) // Validate object is valid, if not create it.
oNewWin[winNum] = window.open('',winNum,'');
else
{
if(oNewWin[winNum].closed) // See if window was closed, if so reopen.
oNewWin[winNum] = window.open('',winNum,'');
else // Otherwise set focus to the called window.
oNewWin[winNum].focus();
}
}
</Script>
<BODY onload=openWin()>
<input type=button OnClick="openFocus(1)" value="Push">
</BODY>
vbasp
February 11th, 2003, 11:24 PM
The work is done by the reply posted above. Thanks a lot.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.