17

I frequent this page: http://stackexchange.com/search

Could the developers please auto-focus the cursor into the google search box that is on this page?

I could do this myself with client-side scripting, but I would rather not. I think this is something that the majority of users would prefer.

0

Couldn't this be done in one line of javascript code?

document.getElementById("gsc-i-id1").focus();

Or

setTimeout(()=>{document.getElementById("gsc-i-id1").focus();},1000);

After 1st comments below:

// untested fastly typed code:
function focusGoogleSearch()
{
    let googleInput = document.getElementById("gsc-i-id1");

    if (googleInput && googleInput.tagName == 'INPUT' && googleInput.type == 'text')
    {
        googleInput.focus();
    }
}

setTimeout(focusGoogleSearch, 1000);
  • This is not so simple. The whole search form is generated on the fly, dynamically, by Google. If you disable JavaScript in your browser, you won't even have a textbox. So such thing would be ugly hack, and might break any day if Google change the id of the textbox. – ShaWiz Mar 9 '18 at 20:50
  • You could use if/then logic to see if the element exists (as named) and test to see if it is a text input and only then focus(); It would never break anything. That id probably hasn't changed since this suggestion was posted. – Lonnie Best Mar 9 '18 at 21:52
  • 1
    Stil, this would be a non elegant hack. Real fix is only if Google change their code to auto focus the search box. – ShaWiz Mar 9 '18 at 22:11
  • 1
    The real hack is that fact that a 3rd party script is embedded into the page to begin with. You should be able to make your own textbox for submitting a simple string to their server. They do it that way so they can monitor all traffic to the page. That's the real hack. I'm just hacking the hack. Anything on top of a hack is a hack. So you're right. – Lonnie Best Mar 9 '18 at 23:56
0

Please don't do this. It would be OK to have a key binding but please do not do it by default. I have always hated websites that auto-focus because it confuses my flow.

  • 3
    So what is your flow in a search page, if not to search for something? – ShaWiz May 5 at 7:11
  • 1
    As long as when I start typing, focus goes to the search-box and doesn't lose one of my keystrokes, I'd be fine with that. I agree with @ShadowWizard ; the page is for searching. So, don't add a step to a user's search flow. – Lonnie Best May 5 at 21:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .