How to Disable the Browser Back Button in JavaScript

Emma Delaney
1 min readJul 20, 2023

--

disable back button in browser using javascript — To disable web browsers’ back button, try to run the following code.

stop the browser back button Example

window.onbeforeunload = function() { return "sorry, Your some work will be lost - really sorry."; };

How to disable the browser back button using JavaScript?

Example 1:

<script type="text/javascript">
function disableBack() { window.history.forward(); }
setTimeout("disableBack()", 0);
window.onunload = function () { null };
</script>

How to Disable Browser Back Button using JavaScript?

Example : 2

window.history.pushState(null, null, window.location.href);
window.onpopstate = function () {
window.history.go(1);
};

Disable browser back button using JavaScript

Example : 3

<script type = "text/javascript" >
function preventBack() { window.history.forward(); }
setTimeout("preventBack()", 0);
window.onunload = function () { null };
</script>

Disable Browser Back Button Functionality using JavaScript

index Page(index.html)

<script type="text/javascript">
function preventBack() { window.history.forward(); }
setTimeout("preventBack()", 0);
window.onunload = function () { null };
</script>
<h3>index</h3>
<hr />
<a href="dashboard.html">Redirect to dashboard</a>

Dashboard Page (dashboard.html)

<h3>Dashboard</h3>
<a href="index.htm">Redirect to index</a>

I hope you get an idea about disable back button in browser using javascript.

--

--

Emma Delaney
Emma Delaney

Written by Emma Delaney

Dubai Based Software Engineer & digital marketing manager Channel : https://bit.ly/3WLAAJZ | https://bit.ly/3wRL2pc

Responses (1)