Javascript to open new window
A classic!
To open a new window in JavaScript, you can use the window.open()
method. Here's a basic example:
window.open("https://www.example.com", "_blank");
This will open a new window with the URL https://www.example.com
and set the target to _blank
, which means the link will open in a new tab or window.
Here are some additional options you can pass to window.open()
:
url
: The URL to open in the new window.windowName
: The name of the window. If you don't specify a name, a default name will be generated.windowFeatures
: A string that specifies the features of the new window. You can use the following values:_blank
: Open the link in a new tab or window._self
: Open the link in the same window._parent
: Open the link in the parent window._top
: Open the link in the full body of the window.width
andheight
: Specify the width and height of the new window.scrollbars
: Specify whether the new window should have scrollbars.resizable
: Specify whether the new window should be resizable.status
: Specify whether the new window should have a status bar.menubar
: Specify whether the new window should have a menubar.toolbar
: Specify whether the new window should have a toolbar.
Here's an example that opens a new window with specific features:
window.open("https://www.example.com", "_blank", "width=800,height=600,scrollbars,resizable");
This will open a new window with a width of 800 pixels, a height of 600 pixels, scrollbars, and the ability to be resized.
Note that some browsers may block pop-ups or have restrictions on opening new windows, so be sure to test your code in different browsers to ensure it works as expected.