Author : Admin
Last Modified : 01-Jul-2020
Complexity : Beginner
How do I redirect to another webpage?
Javascript
In Javascript, you can use window.location.href and window.location.replace(<url>) both.
window.location.href = "<url>" : it keeps the originating page in the session history. you can click back button to open the previous page.
window.location.replace(<url>) : it does not keep the originating page in the session history. means you can not go back.
Example :
// similar behavior as an HTTP redirect window.location.replace("https://gurukultree.com"); // similar behavior as clicking on a link window.location.href = "https://gurukultree.com";
Use
window.location.replace
<script> function OpenURL() { window.location.replace("https://gurukultree.com"); } </script> <button onclick="OpenURL();">Click here</button>
window.location.href = "https://gurukultree.com"
<script> function OpenURL() { window.location.href = "https://gurukultree.com"; } </script> <button onclick="OpenURL();">Click here</button>
jQuery
In jQuery you can use below code
$(location).attr('href','http://gurukultree.com')
For using jQuery you need to include jQuery. So javascript is best option to make code more simple.
Use
<script> function OpenURL() { $(location).attr('href', 'https://gurukultree.com') } </script> <button onclick="OpenURL();">Click here</button>