Javascript Reference - Window focus() Method








The focus() method sets focus to the current window.

Browser Support

focus Yes Yes Yes Yes Yes

Syntax

window.focus()

Parameters

None.

Return Value

No return value.

Example

The following code shows how to set focus to the new window.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<!--  w  ww  .  j  a va 2  s  . c  o  m-->
<script>
function myFunction() {
    var myWindow = window.open("", "", "width=200,height=100");
    myWindow.document.write("<p>A new window!</p>");
    myWindow.focus();
}
</script>

</body>
</html>

The code above is rendered as follows: