All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Open URL In New Tab JavaScript

Last Updated : Mar 11, 2024

Open URL In New Tab JavaScript

In this article we will show you the solution of open URL in new tab JavaScript, in JavaScript with the Window object, we can use two events: open() and close().

In this tutorial, We are going to use a window.open() method here. The syntax for window.open() is,

Window.open(URL,name,specs)

Step By Step Guide On Open URL In New Tab JavaScript :-

In the example below, we are going to see two different examples on window.open() method.

Example 1

<!DOCTYPE html>
<html lang = " en " >
<head>
    <meta charset = " UTF - 8" >
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge " >
    <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " >
    <title> open URL in new tab JavaScript </title>
    <style>
        h1 {
         font-size : larger ;
         font-weight : bolder ;
         color : rgb(113, 221, 113) ;
         text-align : center ;
        }
        h3 {
            text-align : center ;
            font-family : 'Lucida Sans' , 'Lucida Sans Regular' , 'Lucida Grande' , 'Lucida Sans Unicode' , Geneva , Verdana , sans-serif ;
        }
    </style>
</head>
<body>
    <h1> TALKERSCODE </h1>
    <h3> open URL in new tab JavaScript </h3>
    <button onclick="openLink();"> Click! </button>
    <script>
  function openLink() {
    window.open (" https://www.talkerscode.com/");
  }
    </script>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now the <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here.
  7. Creating a <button> with an onclick with a function.
  8. Created the <style> tag to add CSS to the HTML page.
  9. To add JavaScript Create a <script> tag.
  10. Create the function, and within it add window.open(URL)
  11. By clicking on the URL button, we can open the URL in a new tab.

Example 2

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> open url in new tab javascript </title>
    <style>
        h1 {
         font-size : larger ;
         font-weight : bolder ;
         color : rgb(113, 221, 113) ;
         text-align : center ;
        }
        h3 {
            text-align : center ;
            font-family : 'Lucida Sans' , 'Lucida Sans Regular' , 'Lucida Grande' , 'Lucida Sans Unicode' , Geneva , Verdana , sans-serif ;
        }
    </style>
</head>
<body>
    <h1> TALKERSCODE </h1>
    <h3> open url in new tab javascript </h3>
    <div>
        <input type="url" placeholder="enter Url">
        <button> click!! </button>
    </div>
    <script>
        let btn = document.querySelector('button') ;
        let input = document.querySelector('input') ;
        btn.addEventListener('click' , () => {
            window.open(input.value,'_blank') ;
        })
    </script>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now the <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here.
  7. Creating a<input> tag with type ‘text’ and placeholder. Then create a <button> too.
  8. Created the <style> tag to add CSS to the HTML page.
  9. To add JavaScript Create a <script> tag.
  10. Using document.querySelector() for both the <button> and <input> tag.
  11. Add the .addEventListner() tag with ta btn.
  12. window.open(input.value, ‘_blank’) to open the input URL into the new tab. The ‘_blank’ is used here to open the URL in new window or tab.

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to open URLs in a new tab JavaScript.

I hope this article on open URL in new tab JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪