Disable Browser Back Button using JavaScript Function
Hello Friends, “Welcome back” Today we came here with another new article. In this article I will explain about to disable back button of browser.
In this example, i am using two pages, name of first page is Login and the name of second page is Home Page. After Login, User will be redirect to Home page and using Browser Back Button he will not be going back to Login page from Home page.
Disable Browser Back Button JavaScript function
Below Javascript function must be placed inside the Page where you want to disable the Browser Back Button.
For example, here i am creating two pages,first one page is named as Login page and second one page is named as Home page and when User redirect to the Home page you will not redirect him to go to Login page again. So you need to, the following script code must be used inside the Login page.
Inthe preventBack function, the window.history.forward() method is called which navigates the browser forward one step in their history. Whenever Browser Back Button will clicked by the user, this function immediately moves it one step forward.
The setTimeout function schedules to the execute time of the preventBack function with delay of 0 (zero) milliseconds.
Then Finally, the window.onload function returns null.
<script type="text/javascript">
function preventBack() {
window.history.forward();
}
setTimeout("preventBack()", 0);
window.onunload = function () {
null
};
</script>
Implementation of Disable Browser Back Button
Login Page
The HTML Markup of Login page consists of an HTML Anchor tag.
The href property of the Anchor tag is set to name of Home page.
The script to disable the Browser Back Button is placed inside the HEAD section.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Login Page</title>
<script type="text/javascript">
function preventBack() {
window.history.forward();
}
setTimeout("preventBack()", 0);
window.onunload = function () {
null
};
</script>
</head>
<body>
<h3>Login Page</h3>
<hr />
<a href="Home.htm">Redirect to Home</a>
</body>
</html>
Home Page
There is Anchor tag in the HTML Markup of Home page.
The property named href of the Anchor tag is set to name of Login page.
Disabling Browser Back Button
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Home Page</title>
</head>
<body>
<h3>Home Page</h3>
<hr />
<a href="Login.htm"> Redirect to Login Page Click here</a>
</body>
</html>
Comments
Post a Comment