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.



The following two tabs change content below.
Umar Farooque Khan is Software developer and professional blogger serving Worldwide on freelancer and ODesk platform to show the capability of his skills. He is the founder of pTutorial.com. The main purpose of this site is sharing and exchanging of Knowledge related to the recent technologies.