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

window.focus() not working

Status
Not open for further replies.

DotNetGnat

Programmer
Mar 10, 2005
5,548
IN
Hi Guys,

I have something like this on my asp.net page...

Code:
Response.Write("<script>var chatWin=null; if(chatWin!=null) { if(!chatWin.closed) {chatWin.focus();} }else{ chatWin = open('" +url+ "','" +strTarget+ "','scrollbars=no,status=no,resizable=yes,width=600,height=500');}</script>");

everything works fine...i get no errors...i also checked that my browser supports focus method using window.focus

but still the popup wont come into focus...am i doing any simple syntax error...thanks in advance...

-DNG
 
When is chatWin.focus() ever going to get called? If you space out your code so that it's not impossible to see, it's quite apparent that you're never going to hit that condition in your if block:

Code:
<script>

var chatWin = null;
if(chatWin != null) { //[!]this will never be true[/!]
   if(!chatWin.closed) {
      chatWin.focus();
   }
}
else {
   chatWin = open('" +url+ "','" +strTarget+ "','scrollbars=no,status=no,resizable=yes,width=600,height=500');
}

</script>

Did you try putting alerts into your code to see if you're even getting to that part of the if block?

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
you are right...kaht...not sure how i overlooked that one...

i will put alerts as you suggested and will see if it works...thanks

-DNG
 
hmm...ok...now it says object does not support this property or method...so i think focus() method is not supported...

what are my alternatives??

thanks

-DNG
 
now it says object does not support this property or method
On what line of code? When you try to set focus to the window? Is the window even defined at that point? If you're assigning the window to a variable make sure it has global scope so that the function will recognize it (unless you're passing it as a parameter to the function). I'd personally use a try/catch block for what you're doing - it'll make it a lot easier. Copy/paste this in a new file and test for yourself.
Code:
<script type="text/javascript">
var newWindow = null;

function openWindow() {
   try {
      newWindow.focus();
   }
   catch(e) {
      newWindow = window.open("[URL unfurl="true"]http://www.google.com");[/URL]  
   }
}
</script>
<input type="button" value="click me" onclick="openWindow()" />

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
kaht...i changed the code as follows:

Code:
<script>

var chatWin = 0;
if(chatWin != null) { 
   if(!chatWin.closed) {
      chatWin.focus(); [red]//error on this line[/red]
   }
}
else {
   chatWin = open('" +url+ "','" +strTarget+ "','scrollbars=no,status=no,resizable=yes,width=600,height=500');
}

</script>

object does not support this property or method...

-DNG
 
please ignore my postings...i got it solved...its just one of those days with dumb moments...i guess i need some coffee...

thanks guys

-DNG
 
If the latter version is an attempt to rewrite the first version, then [1] mind the definition of url, strTarget etc if they are client side; [2] no need of single quotes. There seems to be quite a bit of confusion there.

Try this first version's revision.
[tt]
Response.Write("<script>var chatWin;chatWin = window.open('" +url+ "','" +strTarget+ "','scrollbars=no,status=no,resizable=yes,width=600,height=500'); setTimeout('chatWin.focus()',1000);"}</script>");
[/tt]
with adhoc 1000 millisec which can be improved leaving less to chance. url and strTarget are server-side (js) variables, I guess. The checking of (chatWin !=null) part seems to be a pointless exercise in this particular write out?
 
[purple]You're welcome[/purple]

On a side note, perhaps you could show what your solution was so that people stumbling across this thread later will vae the solution?

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
i removed all the kind of if conditions that i was trying to put and make the things complicated...i just made it simple as follows:

Code:
<script> 
myOpenWindow('"+url+ "', '"+strTarget+ "' , 'scrollbars=no,status=no,resizable=yes,width=600,height=500');

function myOpenWindow(winURL, winName, winFeatures)
{
theWin = window.open(winURL, winName, winFeatures);
theWin.focus();
return theWin;
} 
</script>

thanks all

-DNG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top