Redirect User to another Page using JavaScript

Updated on     Kisan Patel

If we want to redirect the user from current page to another page using JavaScript, we need to use window.location javascript object.

location.href = "http://www.csharopcode.org";

If we want to current url of the web page, we can get it using location.href as we have done in the alert method.

In below code snippet, we have redirect the user from current page to another page by set the target url to location.href property, in this case we have set to http://www.icsharopcode.org that redirects the user from current page to our website.

<script language="javascript" type="text/javascript">
function TestFunction() {
    alert(location.href);
    location.href = "http://www.csharopcode.org";
}
</script>

<input type="button" id="btnTest" onclick="TestFunction()" value="Click to redirect me" />

See the Pen bdZQed by Kisan (@pka246) on CodePen.


JavaScript

Leave a Reply