Tag Archives: javascript window example

how to open window in javascript onclick

The window.open method in JavaScript

The window.open method is used to open new browser window see the syntax below:-

Syntax


window.open(URL,name,specs,replace)

Note: All the parameter are optional.

Example


<!DOCTYPE html>
<html>
<body>

<p>Click the open new window button to see the result</p>

<button onclick="openWindow()">Open new window</button>

<script>
function openWindow() {
window.open("http://www.ptutorial.com");
}
</script>

</body>
</html>




This is the simple example of browser window in JavaScript. In this script openWindow() function opens new window with www.ptutorial.com. You can easily customize the window using other optional parameter like target, height, width.

Another Example with more params


<!DOCTYPE html>
<html>
<body>

<p>Click the open new window button to see the result</p>

<button onclick="openWindow()">Open new window</button>

<script>
function openWindow() {
window.open("http://www.google.com","", "width=400,height=400");
}
</script>

</body>
</html>



In the above example we have specified the height and width with the window.open method, see the try it yourself button to see the result.