How to Open URL in New Tab using JavaScript?

Tragically, you can’t at present do that – – however you can draw near. You can open another window, and in case you do that without indicating any window aspects or window highlights, most present day programs will open another tab all things considered (contingent upon the client’s inclinations, however at that point, you need to do what the client favors at any rate, right?). So window.open(url) or window.open(url, name) in case you will utilize the name for something. Make certain to do this in direct reaction to a client started occasion, if not the program’s spring up blocker will probably…block the spring up. :- )

Live model

Concerning center around your window…good karma with that. You can call window.focus() after window.open(…), however I would say it doesn’t generally work.

Tossing it out there: If you make what the client interfaces with a certified connection with a URL, the client can choose whether to open it in another tab, another window, whatever and regardless of whether to give it center (in case they’re adequately refined to know Shift+Click and Ctrl+Shift+Click, or the right-click menu).

In HTML, the anchor tag is utilized to open URLs in another tab in a rudimentary and direct way. More with regards to this tag can be gained from this article. Be that as it may, at times there’s a need to do a similar utilizing Javascript. For this situation window.open() strategy ends up being useful. The window.open() strategy is utilized to open another program window or another tab relying upon the program setting and the boundary esteems.

Hello nerd! The consistent arising advancements in the realm of web improvement consistently saves the fervor for this subject through the rooftop. In any case, before you tackle the enormous undertakings, we recommend you start by learning the essentials. Launch your web improvement venture by learning JS ideas with our JavaScript Course. Presently at it’s most minimal cost of all time!

  • To open another tab, we need to utilize _blank in second boundary of window.open().
  • The return worth of window.open() is a reference to the recently made window or tab or invalid assuming that it fizzled.
  • Try not to add a third boundary to it as it will bring about the kickoff of another window rather than a tab

How to open URL in new tab using Javascript

By using window.open() method in javascript you can open any url, to open the URL in new tab use _blank in the window.open() method as second parameter.
for example:
window.open('http://eevibes.com','_blank');
Let us see the complete example:
Click on the Live Demo button to see the output.
Example:
<html lang="en">
    <head>
        <title>Live Demo: How to open URL in new tab using Javascript</title>
    </head>
    <body>
        <div>
            <div>
                <a data-href="http://tutorialsmint.com" href="javascript:void()" id="open_url" title="Click here to open the url in new tab">Click here to open the url in new tab</a>
            </div>
            <br/>
        </div>       
        <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>        
        <script>
            $(document).ready(function () {
                $("#open_url").click(function () {
                    url = $(this).data('href');
                    window.open(url, '_blank');
                });
            });
        </script>
    </body>
</html

HTML <a> target attribute provides an easy and simple way to open the linked URL in the new browser window or tab. You need to use the _blank value in the target attribute to open the linked URL in a new tab or window.

<a href="https://www.codexworld.com" target="_blank">Visit CodexWorld</a>

If you want to open URL with JavaScript, the open() method of Window interface is the best option. The JavaScript window.open() method opens a new browser window. Use _blank in the second parameter of window.open() method to open a URL in a new tab using JavaScript.

The following JavaScript code will open https://www.codexworld.com in a new browser tab or window.

window.open('https://www.codexworld.com', '_blank');

Syntax:

window.open(URL, '_blank');

Example 1:

<html>
 
<body>
 
    <p>Click the button to open a new tab </p>
 
    <button onclick="NewTab()">
      Open Geeksforgeeks
  </button>
 
    <script>
        function NewTab() {
            window.open(
              "https://www.geeksforgeeks.org", "_blank");
        }
    </script>
 
</body>
 
</html>

Example 2:

<html>
 
<body>
 
    <p>Click the button to open google.</p>
 
    <button onclick="Open()">Geeksforgeeks</button>
 
    <script>
        function Open() {
            window.open("https://www.google.com", "_blank");
        }
    </script>
 
</body>
 
</html>

Output :

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.

Also Read: How to get file extension in Python?

Leave a Reply

Your email address will not be published.