yeppe yeppe - 2 months ago 7
HTML Question

Focus is not working in IE even after adding much delay

This question has a reference to the solution posted here. http://stackoverflow.com/a/2600261/5086633

I am facing similar issue in IE but even after adding much delay

200
this is not working as expected in my application. The input box gets focused only intermittently, it skips to focus on the input field even after the page gets rendered in IE. I also tried calling the
setTimeout
function from
$(document).ready(function(){
and
$(window).load(function() {
as specified below with no success.

This code works fine when I test it locally and as a plain html file but not in my application.

Could anyone help me fix /or provide a clue to debug/ this issue in IE, so that input field gets the focus each time the page is rendered in IE?

Footnote:- It is working fine in Firefox (without delay) and in Chrome small delay is required for
focus
to work.

$(document).ready(function(){

setTimeout(function() { document.getElementById('testfocus').focus(); }, 200);

});

$(window).load(function() {
// executes when complete page is fully loaded, including all frames, objects and images
setTimeout(function() { document.getElementById('testfocus').focus(); }, 200);
});




setTimeout(function() { document.getElementById('testfocus').focus(); }, 200);

<html>
<head>
</head>
<body>

<input id="testfocus"/>

</body>
</html>




Answer

Fixed this issue myself played around with this focus delay function, increased it, and found that the input gets the focus when the page is rendered setTimeout(function() { document.getElementById('testfocus').focus(); }, 200); However, I don't know the real reason behind this delay both required in IE and Chrome browsers.