Disable Browser Back Button using jQuery
Introduction: Hello Guys, “Welcome back” Today i am here with another one new great article. In this article I will explain about how to disable user from navigate to previous page using Back Button in Browser using jQuery. On the other hand the back button of the browser will be disabling.
For more Update Please follow us on our Whtsapp Channel
For more Update Please follow us on our Facebook Page
Implementation: In this article I am using two pages, name of first page will be login and name of the second page will be Home page. After login use will be redirect to the Home page. And when user will be redirected to the Home page the back button of the browser will be disable.
Disable Browser Back Button using jQuery
On the HTML page I will be use or place following function inside the Page where you want to prevent the User from coming back using Browser Back Button.
Here I am considering two pages, one will be Login page and other will be Home Page and once User redirect on the Home page after login, you would not want him to go to Login page. So you need to use following script inside the login page. Here I will call the disable_back function inside the window.history.forward() method which navigates the browser forward one step in their history. when Browser Back Button will be clicked by the user, this function immediately moves it one step forward.
The setTimeout method schedules the execution time of the preventBack function with delay of 0 (zero) milliseconds.
Finally, the window.unload function returns null value.
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script type="text/javascript">
function disable_back() {
window.history.forward();
}
setTimeout("disable_back()", 0);
$(window).on("unload", function () {
null;
});
</script>
Disabling Browser Back Button using jQuery
Login Page
On the HTML page named Login I will place an Anchor Tag and its href property ill be set to the name of Home Page. On the HTML page I will inherit the jQuery file named jquery.min.js At the end, the script or the function to disable the Browser Back Button will be placed inside the HEAD tag of HTML page.
<html>
<head>
<title>Login</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script type="text/javascript">
function preventBack() {
window.history.forward();
}
setTimeout("preventBack()", 0);
$(window).on("unload", function () {
null;
});
</script>
</head>
<body>
<h3>Login</h3>
<br /><br />
<a href="Home.htm">Redirect to Home</a>
</body>
</html>
Code of Home Page
The Home View consists of an HTML Anchor tag and its href property of the Anchor link is set to name of Login View.
<html>
<head>
<title>Home</title>
</head>
<body>
<h3>Home</h3>
<br /><br />
<a href="Login.htm">Click here for Login</a>
</body>
</html>
Now, here i am writing the complete code of the both HTML pages. You can copy the whole code and paste the whole code at your HTML page. And enjoy the execution of the whole concept.
Code of Login page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Login</title>
<style type="text/css">
body { font-family: Arial; font-size: 10pt; }
a, a:visited { color: Blue; }
</style>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script type="text/javascript">
function disable_back() {
window.history.forward();
}
setTimeout("disable_back()", 0);
$(window).on("unload", function () {
null;
});
</script>
</head>
<body>
<h3>Login Page </h3>
<br /><br />
<a href="Home.htm">Click here to open Home Page</a>
</body>
</html>
Code of Home page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Home</title>
<style type="text/css">
body { font-family: Arial; font-size: 10pt; }
a, a:visited { color: Blue; }
</style>
</head>
<body>
<h3>Home</h3>
<hr />
<a href="Login.htm">Click here to open Login Page</a>
</body>
</html>
Conclusion: In above code, I explained that about how to disable user from navigate to previous page using Back Button in Browser using jQuery. This code is very helpful for every developer. Bye Bye and take care of you all Developers. We will come back shortly with the new article.
Regards
Using Asp.net
Comments
Post a Comment