Alex Rivera | Logout

Is there any way in JavaScript to focus the document (content area)?

Asked 2011-08-07T23:23:11.890
38

Is there any way to set focus to the document, i.e. the content area, in JavaScript? document.focus() doesn’t seem to do anything.

Edit
Report

1 Answer

55

In HTML 4.01, focus is only discussed in the context of elements such as form controls and links. In HTML 5, it is discussed much more widely. However, how focus works for documents is mostly browser dependent.

You might try:

// Give the document focus
window.focus();

// Remove focus from any focused element
if (document.activeElement) {
    document.activeElement.blur();
}

It is very well supported as well.

answered 2011-08-07T23:42:55.110

Your Answer