JavaScript – Redirect

If you want to redirect web page to another, javascript have simple function for it. Sometimes we need to redirect user to another web page to achieve our goal. We can do this in JavaScript by location function.

Just we need to call this function to redirect our page by firing events. This redirection will be client side and execute on user machine.

Redirecting page in JavaScript can be done by three functions

window.location ="http://www.yahoo.com";

 

window.location.href="http://www.yahoo.com";

 

window.location.replace("http://www.yahoo.com");

location.replace functions will not create entry in history, it just simply edit location with new location URL. This means you cannot go back if you use location replace function.

Full code in html

<html>
<head>
<title>Javascript redirect</title>
<script>
function redirectJS()
{
 //window.location="http://www.yahoo.com";
 // window.location.href="http://www.yahoo.com";
 window.location.replace("http://www.yahoo.com"); //not create entry in history
}
</script>
</head>

<body>
<a href="javascript:redirectJS()">Redirect in Javascript</a>
</body>
</html>

Demo

Bookmark  

 

Leave a Reply

Security Code:

 

  Random Post