Autofocus in the Google Chrome extension

I just made a Chrome extension - search with a form.

When I open the extension, there is no default background setting, but this requires an additional click.

Focus is assumed in the form of popup.html.

Now the JavaScript .focus() method did not work. Is there any other way to set the default focus for Chrome extensions?

HTML:

 <input type="text" id="mydata" /> 

JS:

 document.getElementById('mydata').focus(); 
+2
source share
5 answers

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> 

he works for me.

0
source

the latter chrome supports the tabindex attribute. So you can use

 <input type="text" id="mydata" tabindex="1" /> 

so that it works without any js codes. just fyi.

+4
source

The problem with chrome extensions is that the entire popup.html file does not have focus by default. Therefore, it is simply not enough to focus on the element. First you need to reload the page.

Just put the following code in the page title:

 <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 the body tag:

 <body onload="Init ();"> 

What is it. Please keep in mind that this solution is just a job. Future versions of chrome fix the focus problem in extensions to make this workaround redundant.

+1
source
 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

0
source

Chrome supports autofocus , which does not require JavaScript.

<input type="text" id="mydata" autofocus />

0
source

Source: https://habr.com/ru/post/1400727/


All Articles