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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read