JavaScript Page Redirect

A redirect is when a web page is visited at a certain URL, it changes to a different URL. For example, a person visits “website.com/page-1” in their browser and they are redirected to “website.com/page-2” instead.

This is very useful if we want to shut old website and redirect all the traffic to new website. What we need is for those old links to redirect to the same content on our new site.

JavaScript Redirects

Redirecting to another URL with JavaScript is very easy, we simply have to change the location property on the window object and put this code in head section:

window.location = "http://new-website-name.com";

There are some others ways also to redirect

window.location = "http://new-website-name.com";

window.location.href = "http://new-website-name.com";

window.location.assign("http://new-website-name.com");

window.location.replace("http://new-website-name.com");

Example

<html>
   <head>
      <script type = "text/javascript">
                function Redirect()
                {
               window.location = "http://www.easecod.com";
                }
      </script>
   </head>
   
   <body>
      <p>Click the following button, you will be redirected to new website.</p>
      
      <form>
         <input type = "button" value = "Redirect Me" onclick = "Redirect();" />
      </form>
      
   </body>
</html>