The Wayback Machine - https://web.archive.org/web/20240925235749/https://www.geeksforgeeks.org/how-to-open-url-in-new-tab-using-javascript/
Open In App

How to Open URL in New Tab using JavaScript?

Last Updated : 13 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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() Method

  • To open a new tab, we have to use _blank in the second parameter of the window.open() method.
  • The return value of window.open() is a reference to the newly created window or tab or null if it failed.
  • Do not add a third parameter to it as it will result in the opening of a new window rather than a tab

Syntax:

window.open(URL, '_blank');

Example: In this example, we create a button that, when clicked, uses JavaScript (window.open) to open “geeksforgeeks.org” in a new browser tab, maintaining the current page’s context.

HTML
<html>

<head>
    <title>Open URL in New Tab </title>
</head>

<body>

    <p> Click the button to open
        <b> geeksforgeeks.org </b>
        in new tab
    </p>


    <button onclick="NewTab()">
        Open Geeksforgeeks
    </button>

    <script>
        function NewTab() {
            window.open(
                "https://www.geeksforgeeks.org", "_blank");
        }
    </script>

</body>

</html>

Output:

new-tab

Important Notes:

  • The return value of window.open() is a reference to the newly created window or tab. It returns null if it fails to open the new tab.
  • Avoid adding a third parameter to the window.open() method because it could result in a new window instead of a tab.

Supported Browsers:

The browsers are supported by the window.open() method are listed below:



Similar Reads

How to Open URL in New Tab using Angular ?
In this article, we will learn How to open a link in a new tab using Angular. A Link is a connection from one web page to another web page. Generally, the anchor tag can be used to open URLs in a new tab in an elementary and straightforward manner. However, the window.open() method can also be utilized to open a new browser window or a new tab depe
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 same window and in the same tab using JavaScript: T
4 min read
How to open PDF file in new tab using ReactJS ?
React JS, a commonly used JavaScript library for building user interfaces, sometimes requires opening PDF files in a new tab or window when a button is clicked. You can make this happen by bringing the PDF file into your React application as a module. Prerequisites:React JSFunctional Component in ReactApproach to open PDF File: To complete this tas
2 min read
How to Open a Link in a New Tab in NextJS?
Opening a link in a new tab in Next.js consists of using either the target="_blank" attribute in an anchor (<a>) tag or using Next.js's Link component with the passHref prop to ensure proper handling of routing while opening the link in a new tab. In this article, we will explore both these approaches with complete implementation. We can open
3 min read
How to open a component in a new tab in React JS ?
React Router is a standard library for routing in React. It enables the navigation among views of various components in a React Application, allows changing the browser URL, and keeps the UI in sync with the URL. In this tutorial, you will understand how to open a new component in another tab while residing in the main application. Prerequisites:No
2 min read
How to open URL in a new window using JavaScript?
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 th
3 min read
How to remove active nav-tab when click outside of nav-tab in Bootstrap ?
Bootstrap has the class called "collapse navbar-collapse" which collapses the navigation bar when the user changes the screen resolution. The java-script along-with it at the bottom of the code triggers the collapsed menu in such a way that when the user clicks the hamburger icon of the menu and navigates to the required link, it again collapses th
2 min read
How to Open a Hyperlink in Another Window or Tab in HTML?
Opening hyperlinks in a new window or tab is a common requirement when designing web pages, as it enhances user experience by allowing users to keep the current page open while accessing additional content. In HTML, this can be easily achieved using the `target` attribute of the anchor (`<a>`) tag. By setting `target="_blank"`, you instruct t
2 min read
Convert relative path URL to absolute path URL using JavaScript
Given a relative URL, the task is to convert the relative URL to an absolute URL. Here, the base URL is also given. 2 approaches are discussed here, the first example has the baseURL provided by the user and the second takes it from the URL of the page. Approach 1: Get the relURL and baseURL from user.Use .split() method to split the base and relat
3 min read
How to use protractor to wait for new tab to be created ?
Introduction: Protractor is an end-to-end test framework developed for AngularJS applications, however, it also works for non-Angular JS applications. It runs tests against the application interacting with it as a real user would, running in a real browser. In this article, we are going to use Protractor to check how we can wait for a new tab to be
3 min read
How to get the new image URL after refreshing the image using JavaScript ?
The browser cache relies on the image URL to decide whether the image is the same or not and whether to use the stored version. It means if we change something within the URL and then attempt to reload the image, the cache will no longer be able to change that it is the same resource. The cache will fetch it again from the server. Approach: To chan
2 min read
Happy New Year 2024 | New Year Card Design using HTML CSS and JavaScript
Explore the perfect blend of creativity and technology with our Happy New Year card design tutorial using HTML, CSS, and JavaScript. Read step-by-step guide to craft visually stunning and interactive New Year cards for a memorable celebration. Elevate your web development skills while spreading joy and wishes in style. iframe {width: 100%; height:
7 min read
What is URL Locator & what are common schemes used for the URL in HTML ?
Every HTML document on the internet has a unique URL. URL stands for Uniform Resource Locator. The URL is the HTML document's address on the internet. Any file that can be accessed via the internet has a URL, including images, JavaScript files, CSS files, Flash files, and so on. To view an HTML document in a browser, enter the URL of the document i
2 min read
PHP | fopen( ) (Function open file or URL)
The fopen() function in PHP is an inbuilt function which is used to open a file or an URL. It is used to bind a resource to a steam using a specific filename. The filename and mode to be checked are sent as parameters to the fopen() function and it returns a file pointer resource if a match is found and a False on failure. The error output can be h
4 min read
How to make curved active tab in navigation menu using HTML CSS & JavaScript ?
In this article, we will learn about the curved outside in the active tab used in the navigation menu using HTML, CSS & JavaScript. One of the most beautiful and good-looking designs of a navigation menu is the 'Curve outside in Active Tab' design. With the help of the CSS border-radius property, it is very easy to make an inside curve. But whi
6 min read
How to Create Tab Headers using CSS & JavaScript?
Tab headers are a commonly used user interface element in web development. They provide a way to organize content into distinct sections, allowing users to switch between them easily. Each tab represents a different category or topic, and clicking on a tab displays the corresponding content while hiding the others. Prerequisites:HTMLCSSJavaScriptSt
2 min read
How to close current tab in a browser window using JavaScript?
In this article, we will see how to close the current tab in a browser window using JavaScript. window.close() methodTo make this, we will use window.close() method.  This method is used to close the window which is opened by the window.open() method.  Syntax:window.close();But accounting for a security feature, that was introduced a few years ago,
1 min read
How to assign custom URL domain Web Address in New Google Sites?
One way to enhance your website's credibility and brand recognition is by assigning a custom URL domain. This is especially true for users of Google Sites, a versatile and user-friendly website builder that allows for seamless integration with other Google Workspace tools. You may be thinking that you can't host the Google Site website that you mad
4 min read
Which attribute is used to target the webpage to open into a new window in HTML ?
HTML (Hyper Text Markup Language) plays the most significant role in website development. But when it comes to good user experience and easy navigation then the <a> tag is used. In this article, we will discuss the hyperlink tag, its possible attributes, and how a user can open a web page in a new window in HTML. IntroductionThe target attrib
2 min read
How to return true if browser tab page is focused in JavaScript ?
There may be situations when we need to check if a browser tab is focused or not. The reasons for this include: Prevent sending network requests if the page is not being used by the user as this would reduce the amount of load on the server. Also, this would save upon the cost if we are using a paid third-party API.Stop playback of media when the t
4 min read
How to search a vertical tab character with JavaScript RegExp ?
In this article, we will see how to search a vertical tab character with JavaScript RegExp. A vertical tab character (VT) is a control character used to cause a printer or display device to move its output up by one line. It has an ASCII code of 11 or 0x0B.In JavaScript regular expressions, the vertical tab character can be searched using its escap
3 min read
How to detect browser or tab closing in JavaScript ?
Detecting browser or tab closure in JavaScript is essential for preventing data loss or unintended navigation. Using the beforeunload event, developers can prompt users with a confirmation dialog, ensuring they don't accidentally leave a page with unsaved changes or important information. The beforeunload event is triggered just before the window i
2 min read
New self vs. new static in PHP
New self: The self is keyword in PHP. It refers to the same class in which the new keyword is actually written. It refers to the class members, but not for any particular object. This is because the static members(variables or functions) are class members shared by all the objects of the class. The function called self::theFunction() behaves like "
4 min read
How to force tab-navigation to stay in place using Bootstrap ?
Tab-navigation: Tabs are made with <ul class="nav nav-tabs"> and we use the <li class="active"> element to mark the current page. To fix the position of navigation tab style, position: fixed; property is used. Syntax: <ul class="nav nav-tabs" id="myTab" role="tablist"> <li class="nav-item"> <a class="nav-link active" href
3 min read
How to check which tab is active using Material UI ?
Material-UI is one of the most popular React UI libraries. Material-UI components work in isolation. They are self-supporting, and will only inject the styles they need to display. They don't rely on any global style-sheets such as normalize.css. Some examples of Material UI components are Dialog, Tabs, Text Field, Menu, Chip, Card, Stepper, Paper.
4 min read
ReactJS Evergreen Tab Component
React Evergreen is a popular front-end library with a set of React components for building beautiful products as this library is flexible, sensible defaults, and User friendly. Tab Component allows the user to switch between components present in given different tabs. It is used for organizing our content in logical groups. We can use the following
2 min read
How to Create a Tab Image Gallery ?
To create a tab image gallery you need to use HTML, CSS, and JavaScript. HTML will make the structure of the body, and CSS will make it looks good. This kind of tab image gallery looks attractive on a website. By using JavaScript you can easily change the displayed pictures from the gallery. Creating Structure: In the HTML section, we will create a
2 min read
Semantic-UI | Tab
Semantic UI is an open-source framework that provides user-friendly tabs to create great user interfaces for websites. It is very much similar to bootstrap usages for creating amazing websites. It uses a class to add styles to different elements of the HTML page structure. A tab is a hidden section of content that can be viewed by clicking on it. Y
3 min read
How to make bootstrap version 2 tab dropdown?
Using a simple Bootstrap plug-in, you can add dropdown menu in tabs by using the dropdown class. The nav and nav-tabs classes are also used to achieve this. First, make an unordered list and give it a class of nav and nav-tabs. Now, give a class of dropdown to the list item tag which you wish to behave as a dropdown. The next step is to create an u
3 min read
How to set active tab style with AngularJS ?
In this article, we will see how to set an active tab style using AngularJS, & will also understand its implementation through the example. This task can be accomplished by implementing the isActive and the ng-controller method. We will perform this task with 2 different methods. Method 1: The ng-controller Directive in AngularJS is used to add
2 min read
Article Tags :