Redirect web page to another page using javascript

In this example we will see how to redirect one web page to another web page using javascript. we need to set the redirect url to window.location.href, then the web page will be redirect to given url.

Syntax: window.location.href = "url here";


Redirect this web site to

  • If we give the relative path as url, then the page will be redirected to another page with in the web site.
    • ex : if we give the url as "/linq" then the page will be redirect to "/linq"
  • we can give the another website url, so that it will redirect to another web site.
    • ex:

Source Code

<html> <head> <title></title> <script language="javascript"> function redirectwebsite() { var url = document.getElementById("txtwebsiteurl").value; window.location.href = url; } </script> </head> <body> Redirect this web site to <input type="text" id="txtwebsiteurl" value="" style="width: 410px;" /> <input type="button" value="Redirect" onclick="redirectwebsite();" /> </body> </html>