admin管理员组文章数量:1421572
I faced a very interesting issue.
I'm trying to set the focus on a input field using Javascript (no jQuery, I tried that also but not worked) using window.onLoad
.
Just take a look at this fiddle : setFocusOnLoad
It's working fine in chrome browser but not in Firefox. Is there any issue in Firefox? How can I resolve it.
Edited:
Here is the code I copied in html file:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function onloadFocus(){
var name = document.getElementById('name');
// name.value='window.onload called';
name.focus();
}
window.onload=onloadFocus();
</script>
</head>
<body>
<div><input type='text' value='' id='name' name='name'></div>
</body>
</html>
I faced a very interesting issue.
I'm trying to set the focus on a input field using Javascript (no jQuery, I tried that also but not worked) using window.onLoad
.
Just take a look at this fiddle : setFocusOnLoad
It's working fine in chrome browser but not in Firefox. Is there any issue in Firefox? How can I resolve it.
Edited:
Here is the code I copied in html file:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function onloadFocus(){
var name = document.getElementById('name');
// name.value='window.onload called';
name.focus();
}
window.onload=onloadFocus();
</script>
</head>
<body>
<div><input type='text' value='' id='name' name='name'></div>
</body>
</html>
Share
Improve this question
edited Jan 12, 2021 at 9:17
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Sep 22, 2012 at 10:16
Ravikumar SharmaRavikumar Sharma
3,7405 gold badges38 silver badges61 bronze badges
7
- 2 jsfiddle sets the focus later on the javascript-frame, try the standalone-page and it works well: fiddle.jshell/ravi441988/r5Rgr/4/show – Dr.Molle Commented Sep 22, 2012 at 10:26
- @Dr. Molle > It's not working on page :( – Ravikumar Sharma Commented Sep 22, 2012 at 10:32
- @ravi he have given example and its working see it i have also used this many of places – Gyan Chandra Srivastava Commented Sep 22, 2012 at 10:34
- @Gyan Chandra Srivastava> I tried also looked link it's working fine when I copied fiddel in html page it is not working. – Ravikumar Sharma Commented Sep 22, 2012 at 10:41
- We cannot solve the issue when we don't know what happens on your page. My guess is that window.onload is overwritten somewhere, use addEventListener( developer.mozilla/en-US/docs/DOM/element.addEventListener ) instead. – Dr.Molle Commented Sep 22, 2012 at 10:46
4 Answers
Reset to default 6Try adding a slight delay:
function onloadFocus(){
setTimeout(function() {
document.getElementById('name').focus()
}, 10);
}
Update jsFiddle
You must wrap the function-call into a function, otherwise it will be called immediately, not onLoad(the input is still unknown at this time):
window.onload=function(){onloadFocus();}
you should use window.onload=onloadFocus;
instead of window.onload=onloadFocus();
because in case of window.onload=onloadFocus();
onloadFocus will run immediately and at that time input field may not be available.
jsfiddle
I got the solution for it. If you want to focus in Firefox. Write the focus function at the starting of the script tag.
<script type="text/javascript">
document.getElementById('name').focus();
// rest of the code here.
</script>
Hope this will help you.
本文标签: javascriptNot setting focus to text field in FirefoxStack Overflow
版权声明:本文标题:javascript - Not setting focus to text field in Firefox - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745352013a2654825.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论