In my application i want to disable browser back button. And redirect my page to another page if user clicks refresh button present in the browser.
Loading
In my application i want to disable browser back button. And redirect my page to another page if user clicks refresh button present in the browser.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Raj BhattRank #513 · 4.8K reputationPosted Mar 24, 2023, 2:39 AM EDT
You can try below code:
$(document).ready(function() { function disableBack() { window.history.forward() } window.onload = disableBack(); window.onpageshow = function(e) { if (e.persisted) disableBack(); } });Disabling browser back button Next page $(document).ready(function() { function disableBack() { window.history.forward() } window.onload = disableBack(); window.onpageshow = function(e) { if (e.persisted) disableBack(); } });Vishal JoshiRank #377 · 6.7K reputationPosted Mar 24, 2023, 3:27 AM EDT
Hello Gopi
There are different ways to achieve the requirement.
Sample code to disable back button programmatically
$(document).ready(function() { function disableBack() { window.history.forward() } window.onload = disableBack(); window.onpageshow = function(e) { if (e.persisted) disableBack(); } });Thanks
Vishal YelveRank #178 · 13.6K reputationPosted Mar 24, 2023, 3:29 AM EDT
Try this,
$(document).ready(function() { window.history.pushState(null, "", window.location.href); window.onpopstate = function() { window.history.pushState(null, "", window.location.href); }; });Deepak RawatRank #646 · 3.7K reputationPosted May 12, 2023, 6:55 AM EDT
// Listen for the refresh button click event window.onbeforeunload = function() { // Redirect the page to another URL window.location.href = 'https://example.com/redirect'; // Return a message to display to the user (optional) return 'Are you sure you want to refresh?'; };Tuhin PaulRank #7 · 71.9K reputationPosted May 12, 2023, 11:16 PM EDT
You can disable the browser back button and redirect the user to another page if they click the refresh button by using the following code:
function disableBackButton() { window.history.pushState({}, "", window.location.href); } function redirectOnRefresh() { window.location.href = "https://example.com/new-page"; } window.addEventListener("popstate", disableBackButton); window.addEventListener("beforeunload", redirectOnRefresh);This code will disable the browser back button by pushing the current page to the browser's history stack. When the user clicks the back button, the page will be restored from the history stack instead of going back to the previous page. The code will also redirect the user to a new page if they click the refresh button. This is done by listening for the beforeunload event and returning a string that tells the browser to redirect the user to the new page. You can modify this code to fit your specific needs. For example, you could add a message to the beforeunload event to warn the user that they are about to be redirected.