Use focus() method places focus on the window in JavaScript

Description

The following code shows how to use focus() method places focus on the window.

Example


<html>
<script language="JavaScript">
function openWin(){<!--  www .  j  av a  2s.c  om-->
var myBars = 'directories=no,location=no,menubar=no,status=no';
myBars += ',titlebar=no,toolbar=no';
var myOptions = 'scrollbars=no,width=400,height=200,resizeable=no';
var myFeatures = myBars + ',' + myOptions;
newWin = open('', 'myDoc', myFeatures);
newWin.document.writeln('Here is the child window');
newWin.document.close();
self.focus();
}
</script>
<body>
<form>
<input type=BUTTON value="Open" onClick='openWin()'>
</form>
</body>
</html>

Click to view the demo

The code above generates the following result.

Use focus() method places focus on the window in JavaScript