Disable browser back button using jquery?
I would like to disable the back button when the user clicks on unload for this. I only delete this user's cookies, not the session in my application. And you want to disable the back button
if(getCookie('username') == ""){
Disable back button;// we use "window.history.redirect" for javascript i look jquery for this
}
Please help me..
source to share
CODE:
<HTML>
<HEAD>
<SCRIPT type="text/javascript">
window.history.forward();
function noBack() {
window.history.forward();
}
</SCRIPT>
</HEAD>
<BODY onload="noBack();" onpageshow="if (event.persisted) noBack();" onunload="">
</BODY>
</HTML>
source to share
var path = 'Test.aspx'; //write here name of your page
history.pushState(null, null, path + window.location.search);
window.addEventListener('popstate', function (event) {
history.pushState(null, null, path + window.location.search);
});
This works in all browsers (except <IE10), even if the urls contain query strings.
You don't even need jquery for this.
look here to apply this to your page.
source to share
Use the following code as :
window.onload = function () {
if (typeof history.pushState === "function") {
history.pushState("jibberish", null, null);
window.onpopstate = function () {
history.pushState('newjibberish', null, null);
};
}
else {
var ignoreHashChange = true;
window.onhashchange = function () {
if (!ignoreHashChange) {
ignoreHashChange = true;
window.location.hash = Math.random();
}
else {
ignoreHashChange = false;
}
};
}
};
source to share
This isn't exactly your approach, but you can disable the navigation bar of any window using simple javascript. Just set window.menubar
and window.toolbar
visibility to false
like this:
window.menubar.visible = false ;
window.toolbar.visible = false ;
Update:
Changing the visibility of the menu and toolbar of an existing window appears to violate security protocols. ( https://developer.mozilla.org/en/DOM/window.menubar )
So the only real way is to open a new window with a menu and a toolbar set to "none" in the first place:
window.open(url,name,"menubar=no,toolbar=no[, additional options]",true) ;
If you set the replace argument (the last argument) to true, the new window must also inherit the history of the window in which it was opened.
Check out https://developer.mozilla.org/en/DOM/window.open and http://msdn.microsoft.com/en-us/library/ms536651(VS.85).aspx for reference.
source to share
Sunil was right, but to use jQuery, paste the code below on any page you want to prevent. I tested this in IE 11, chrome and FF which worked great.
$(document).ready(function () {
function disableBack() {window.history.forward()}
window.onload = disableBack();
window.onpageshow = function (evt) {if (evt.persisted) disableBack()}
});
source to share