admin管理员组文章数量:1291594
I made a simple Chrome extension - search with a form.
When I open the extension, there is no default focus on form, but it requires additional click.
Focus is suppose to happen on popup.html form.
Now JavaScript .focus()
method didn't work, Is there any other way to set default focus for Chrome extensions?
HTML:
<input type="text" id="mydata" />
JS:
document.getElementById('mydata').focus();
I made a simple Chrome extension - search with a form.
When I open the extension, there is no default focus on form, but it requires additional click.
Focus is suppose to happen on popup.html form.
Now JavaScript .focus()
method didn't work, Is there any other way to set default focus for Chrome extensions?
HTML:
<input type="text" id="mydata" />
JS:
document.getElementById('mydata').focus();
Share
Improve this question
edited Jul 6, 2011 at 13:13
dd .
asked Jul 6, 2011 at 9:15
dd .dd .
5722 gold badges13 silver badges28 bronze badges
1
- It is suppose to happen on popup.html form. – dd . Commented Jul 6, 2011 at 13:15
5 Answers
Reset to default 4the latest chrome has supported tabindex
attribute. So you can use
<input type="text" id="mydata" tabindex="1" />
to make it work without any js codes. just FYI.
Chrome supports autofocus which doesn't require any JavaScript.
<input type="text" id="mydata" autofocus />
The issue with chrome extensions is that the entire popup.html document has no focus by default. Therefore it's simply not enough to set the focus on an element. You first need to reload the page.
Just put the following code into the page header:
<script type="text/javascript">
function Init () {
if (document.hasFocus)
setInterval ("checkFocus ()", 300);
}
function checkFocus () {
if (!document.hasFocus ()) {
document.location.reload();
}
}
document.getElementById('mydata').focus();
</script>
Then call the code in your body tag:
<body onload="Init ();">
That's it. Please keep in mind that this solution is just a work-around. May future chrome versions fix the focus issue in extensions to make this workaround superfluous.
Try something like this:
<html>
<head>
<script type="text/javascript">
function setFocus()
{
document.getElementById("target").focus();
}
</script>
</head>
<body onload="setFocus()">
<input type="text" id="target" name="target" size="25" value="" />
</body>
</html>
it works for me.
function setFocus(loc) {
window.open(loc, 'popup1', 'height=655,width=710,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no,minimizable=no').close();
window.open(loc, 'popup1', 'height=655,width=710,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no,minimizable=no').status.fixed();
}
Then:
lnkSongTitle.Attributes.Add("onclick", "javascript:setFocus('url')");
or in html onClick
call SetFocus() function
本文标签: javascriptAuto Focus in Google Chrome extensionStack Overflow
版权声明:本文标题:javascript - Auto Focus in Google Chrome extension - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741532312a2383829.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论