How to open URL in a new window using JavaScript?
Last Updated :
01 Aug, 2024
In HTML, the anchor tag (<a>
) is used to open new windows and tabs in a very straightforward manner. However, there are situations where you need to achieve the same functionality using JavaScript. This is where the window.open()
method becomes useful.
The window.open()
method is used to open a new browser window or a new tab, depending on the browser settings and the parameter values provided. Here’s how you can use it:
Syntax
window.open(URL, name, specs, replace);
Note: All the parameters are optional.
There are several methods to open the URL in a new window using JavaScript which are as follows:
Using Window.Open() method
To open a URL in a new window, make sure that the second parameter is not _blank. The other parameters can be varied accordingly as per the need of the new window.
Example: To demonstrate creating a button and setting an onclick function to open a new window.
HTML
<!DOCTYPE html>
<html>
<head>
<title>
Click the button to
open a new window.
</title>
</head>
<body style="text-align:center;">
<h1 style="color:green">
GeeksforGeeks
</h1>
<p>
Click the button to
open a new window.
</p>
<button onclick="NewTab()">
Open Geeksforgeeks
</button>
<script>
function NewTab() {
window.open("https://www.geeksforgeeks.org",
"", "width=300, height=300");
}
</script>
</body>
</html>
Output:
Using Anchor tag
The anchor tag (<a>
) can also be utilized with JavaScript to open URLs in new windows or tabs. By specifying target="_blank"
, the link will open in a new tab. This approach is straightforward and commonly used for external links, enhancing user experience by keeping the current page intact.
Example: Use Anchor tag to open URL in a new window using JavaScript.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Using anchor tag</title>
</head>
<body style="text-align:center;">
<h1 style="color:green">
GeeksforGeeks
</h1>
<p>
Click the button to open
GeeksforGeeks in a new window.
</p>
<a onclick='window.open("https://www.geeksforgeeks.org/",
"_blank", "width=300, height=300");'>
GeeksforGeeks
</a>
</body>
</html>
Output:
Another approach involves using the input tag (<input type="button">
) with an onclick
event handler to open URLs in new windows. Similar to the anchor tag, this approach allows for customizing window properties such as width, height, and position. It provides a clickable button for users to initiate the action.
Example: To demonstrate using the Input tag to open the URL in a new window.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Using input tag</title>
</head>
<body style="text-align:center;">
<h1 style="color:green">
GeeksforGeeks
</h1>
<p>
Click the button to open
GeeksforGeeks in a new window.
</p>
<input type="button" onclick="window.open(
'https://www.geeksforgeeks.org/','geeks',
'toolbars=0,width=300,height=300,left=200,top=200,scrollbars=1,resizable=1');"
value="Open the window">
</body>
</html>
Output:
Conclusion
Using JavaScript to open URLs in a new window or tab is a simple yet powerful functionality provided by the window.open()
method. This method offers flexibility, allowing you to specify the URL, name, and features of the new window, making it suitable for a wide range of applications. Whether you need to enhance user experience by opening external links in new tabs or control the behavior and appearance of new browser windows, window.open()
provides the necessary tools to achieve these goals effectively.
Supported Browsers:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
HTML is the foundation of web pages and is used for webpage development by structuring websites and web apps. You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.
JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.
Similar Reads
How to Open URL in New Tab using JavaScript?
HTML anchor tag is used to open URLs in a new tab using the target="_blank" attribute. In JavaScript, we have an inbuilt method window.open() which is used to open a new browser window or new tab depending on the browser settings. Using window.open() MethodTo open a new tab, we have to use _blank in
2 min read
How to open URL in same window and same tab using JavaScript ?
Opening a URL in the same window and in the same tab is a common requirement for web developers. This functionality is useful when you want to load a new web page or document into the current browser window and replace the content of the current web page. There are several ways to open URLs in the s
4 min read
How to parse URL using JavaScript ?
Given an URL and the task is to parse that URL and retrieve all the related data using JavaScript. Example: URL: https://www.geeksforgeeks.org/courses When we parse the above URL then we can find hostname: geeksforgeeks.com path: /courses Method 1: In this method, we will use createElement() method
2 min read
How to print a web page using JavaScript ?
Javascript is the world most popular lightweight, cross-platform, interpreted compiled programming language, along with a scripting language for web. It facilitates the dynamic functionality that helps to make the interactive website. As all the Morden browsers use the Javascript & is a client-s
2 min read
How to Open a Link Without Clicking on it using JavaScript?
To open a link without clicking on it we can use the onmouseover method of JavaScript. The link will open when the mouse moves over the text. It returns a newly created window, or NULL if the call gets failed. Syntax:window.open( URL, name, Specs )Note: Allow Pop-up of Web Browser. Example 1: URL is
2 min read
How to open web cam in JavaScript ?
In this article, we will see how to open a webcam and show a live video using JavaScript. For this, we are going to use Navigator Media Devices. Navigator Media DevicesIt is a read-only property that returns a Media Devices object, which helps us to access the connected media input devices like the
2 min read
How to replace plain URL with link using JavaScript ?
Given a plane URL, the task is to replace the plain URLs with the links. This problem can be solved with the help of Regular Expressions. Approach: Using RegExp and replace() MethodRegExp - This looks for the URL in the provided text.This RegExp parses the URL and puts the address to the $1 variable
2 min read
How to close window using JavaScript which is opened by the user with a URL ?
JavaScript does not allow one to close a window opened by the user, using the window.close() method due to security issues. However, we can close a window by using a workaround. The approach to be followed is by opening the current URL using JavaScript so that it could be closed with a script. Synta
2 min read
How to redirect browser window back using JavaScript ?
Redirecting a browser window back to the previous page using JavaScript is a common task for web developers looking to enhance user navigation. This guide will cover the simple yet important methods available in JavaScript to achieve this functionality, ensuring a smooth user experience. There are s
2 min read
How to enable JavaScript in Windows ?
Whenever you are browsing through some websites, you can observe that some of the blocks are not displayed properly. Instead of behaving dynamic, they are just standstill like any other static page which is very undesirable to the user. This behavior is mainly because of not enabling JavaScript on y
2 min read