views:

433

answers:

3

Hi all, I want to focus on a special textbox when page is loaded. I do this with js function focus() and it works fine. But when I open the page in a new tab in firefox, it doesn't focus on the textbox. any idea?

+3  A: 

Firefox open in new tab opens in a background tab. If the page itself doesn't have focus, then it can't pass its focus to any element within the page.

In order to acheive the desired effect, you will need to set the focus on the control when the document first gains focus.

Paul Butcher
A: 

I agree with Mr.Paul. Here is the simple example

<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>  
  <script language="javascript">
  <!--
function SetFocus()
{
    document.getElementById('t1').focus();
}

  //-->
  </script>
 </HEAD>

 <BODY onLoad="SetFocus()">
<input type="text" id="t1">
<input type="text" id="t2">
 </BODY>
</HTML>

Setting the focus on the first textbox.Whenever the new tab opens, set the proper url and the first textbox will gain the focus.

Moreover, when the new tab is clicked, the url is not set to the actual url of the file. After setting the url you will definitely find the textbox1 is having the focus.

priyanka.sarkar
Still doesn't work. but when I put alert before focus() function, it works ! I really don't know Y !
Ali Bozorgkhan
Have you set the url properly
priyanka.sarkar
Putting an alert before the call to focus will focus that tab, and so the focus call will work.
sdwilsh
Thanx, It works, but I don't want to alert, because I am writing this project for a non programmer person who is not familiar with computer and I want it to be simple.
Ali Bozorgkhan
+2  A: 

yep. It doesn't work in firefox. i dunno why. But it works, if we do the following in firefox. go to Tools --> Options click on Tabs. Check the option "When i open a link in a new tab, switch to it immediately".

This will take you to the opened tab directly and onfocus works. This is not the solution. This will work only on your firefox ofcourse. can't guarantee users have that option checked. :(

tony_le_montana
Thank u. My server and client are same systems. It worked great. thanx !!!
Ali Bozorgkhan