How to redirect page through Javascript, Jquery code. Redirection means taking user to new location. Let’s check some Javascript and Jquery code to redirect a page.
Reload Page through Javascript and Jquery.
How to Redirect Page through Javascript, Jquery
1. Redirect page on Checkbox check
I have a checkbox and i want to redirect a page on some particular link when checkbox is checked. So how to do it.
Create one checkbox
1 |
<input type="checkbox" name="redirect" id="checkbox_redirect" value="1" /> |
Jquery snippet to redirect a page to webrewrite.com when user checked a checkbox.
1 2 3 4 5 6 |
$("#checkbox_redirect").click(function() { var url = "https://webrewrite.com"; window.location.href = url; }); |
Alternative Method.
1 2 3 4 |
$("#checkbox_redirect").click(function() { var url = "https://webrewrite.com"; $(location).attr('href',url); }); |
2. Redirect page on button click.
1 |
<input type="button" value="Click" id="bt_redirect" /> |
Jquery snippet.
1 2 3 4 5 |
$('#bt_redirect').click(function() { var url = "https://webrewrite.com"; window.location.href = url; }); |
OR
1 |
<input type="button" value="Click" id="bt_redirect" onclick="redirect()" /> |
On the button onclick event we call redirect() method. Define the redirect method.
1 2 3 4 |
function redirect(){ var url = "https://webrewrite.com"; window.location.href = url; } |
Use window.location.replace to Redirect Page
1 2 3 4 5 |
$("#checkbox_redirect").click(function() { window.location.replace("https://webrewrite.com"); }); |
So how window.location.replace is different from window.location.href.
replace method navigates to the mentioned URL without adding a new record to the history.
NOTE: replace method does not keep originating page in session history, which may affect behaviour of back button.