The focus() method is used to give focus to a textbox, or other html components. Here is a simple example to make a textbox focus automatically after page load.


 




 

%MINIFYHTMLf0fc5ce8d877612bb831b1ecec0cc4a70%

The above code is working fine in Firefox (FF), but not in Internet Explorer (IE). Actually the IE is supporting the focus() method, we just need to use other way to write the focus method.

Base on my personal observation, this is because IE run the focus() method before the textbox render properly. However the Javascript is put after the textbox, the focus() method suppose to execute after textbox fully render itself, may be IE treat it as different ways? Ya, IE always have “IE-ONLY” implementation…

Here is an unofficial solution, we have to use the setTimeout() fucntion to delay the focus() execute time.

setTimeout(function() { document.getElementById('myInput').focus(); }, 10);

After delay the execute time, the focus() is working fine in IE , or even FF.


 




 

%MINIFYHTMLf0fc5ce8d877612bb831b1ecec0cc4a71%

Reference

setTimeout() – http://www.w3schools.com/htmldom/met_win_settimeout.asp