Click button to popup new window javascript
A classic!
To open a new window using JavaScript when a button is clicked, you can use the window.open()
method. Here's an example:
<button onclick="openNewWindow()">Click me!</button>
<script>
function openNewWindow() {
window.open("https://www.example.com", "_blank", "width=400,height=300");
}
</script>
In this example:
window.open()
is the method that opens a new window."https://www.example.com"
is the URL of the page you want to open in the new window."_blank"
specifies that the new window should be opened in a new tab or window."width=400,height=300"
sets the width and height of the new window.
When the button is clicked, the openNewWindow()
function is called, which opens a new window with the specified URL and dimensions.
You can also add additional parameters to the window.open()
method to customize the new window further. For example:
"_blank"
can be replaced with"noopener"
or" noreferrer"
to prevent the new window from being able to access the parent window.- You can add additional attributes to the
window.open()
method, such astoolbar
,menubar
,scrollbars
, etc. to customize the appearance of the new window.
Here's an updated example:
<button onclick="openNewWindow()">Click me!</button>
<script>
function openNewWindow() {
window.open("https://www.example.com", "_blank", "width=400,height=300,toolbar=no,menubar=no,scrollbars=yes");
}
</script>
In this example, the new window will have a width of 400 pixels, a height of 300 pixels, no toolbar or menubar, and scrollbars enabled.