The focus()
method sets focus to the current window.
focus |
Yes | Yes | Yes | Yes | Yes |
window.focus()
None.
No return value.
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: