Friday, September 5, 2025
HomeLanguagesJavascriptJavaScript | Redirect a URL

JavaScript | Redirect a URL

Prerequisites:

  • Learn Basic HTML
  • Learn Basic Javascript
  • Redirecting a URL in JavaScript is nothing but sending the user from a URL to another URL. In Javascript, window.location function is used to redirect to a URL.

    JavaScript codes for redirecting to a URL:
    Code #1:




    <!DOCTYPE html>
    <html>
    <head>
        <title>Redirect url in Javascript</title>
    </head>
    <body>
    <script>
        var url= "https://www.geeksforgeeks.org";
        window.location = url;
    </script>
    </body>
    </html>

    
    

    Output:

    Code #2:
    Below code takes an input url from the user and the url is stored in a variable through getElementById() and window.location takes the page to that url.




    <!DOCTYPE html>
    <html>
    <head>
        <title>Redirect url in Javascript</title>
    </head>
    <body>
    <input id = "url" type = "text" name = "url" 
                            placeholder = "Enter a url here">
    <input type = "submit" name = "button" onclick = "fun()">
    <script>
    function fun() {
        var url= document.getElementById("url").value;
        document.write("Redirecting to the url in 3 seconds...");
        setTimeout(function(){window.location = url;}, 3000);
    }
    </script>
    </body>
    </html>

    
    

    Output:

    At first this will be the output, and then if the link like (https://www.geeksforgeeks.org/) is put inside the box, it will redirect to the home page of the neveropen and will be shown like below.

    Steps for executing above codes:

    • Save these files with .html extension as file_name.html
    • Then open the files in a browser, it will get redirect to the neveropen homepage.
    RELATED ARTICLES

    Most Popular

    Dominic
    32269 POSTS0 COMMENTS
    Milvus
    81 POSTS0 COMMENTS
    Nango Kala
    6638 POSTS0 COMMENTS
    Nicole Veronica
    11802 POSTS0 COMMENTS
    Nokonwaba Nkukhwana
    11866 POSTS0 COMMENTS
    Shaida Kate Naidoo
    6752 POSTS0 COMMENTS
    Ted Musemwa
    7027 POSTS0 COMMENTS
    Thapelo Manthata
    6704 POSTS0 COMMENTS
    Umr Jansen
    6721 POSTS0 COMMENTS