Discuss Is there a way to DISABLE the BACK BUTTON? in the JavaScript Development forum on Dev Articles. Is there a way to DISABLE the BACK BUTTON? JavaScript Development forum discussing the use of JavaScript and its features as a powerful DOM manipulator. JavaScript is used in most websites to enrich the interface and enhance the user experience.
Posts: 39
Time spent in forums: 4 h 34 m 34 sec
Reputation Power: 16
Is there a way to DISABLE the BACK BUTTON?
I have a multiple page regstration process for a site that I am developing. In initial beta-testing, I have found some users (even though they are warned not to) have been using the back button in the middle of registration - thereby wreaking havoc on the registration process!
I was wondering if there is a way to disable the back button?
Posts: 785
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 16
Use this Javascript method:
replace Method
The replace method replaces the current History entry with the specified URL. After calling the replace method, you cannot navigate back to the previous URL using the browser's Back button.
Syntax: location.replace(URL)
__________________
__________________________________________________ _
Wil Moore III, MCP | Integrations Specialist | Senior Consultant Are You Listed...? | DigitallySmooth Inc.
Posts: 785
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 16
For the method iahmed proposed, you need to put that code at the bottom of each page that you DON'T want the user to be able to go back to or resubmit.
Posts: 1
Time spent in forums: 19 m 50 sec
Reputation Power: 0
Quote:
Originally Posted by Mojoman
I have a multiple page regstration process for a site that I am developing. In initial beta-testing, I have found some users (even though they are warned not to) have been using the back button in the middle of registration - thereby wreaking havoc on the registration process!
I was wondering if there is a way to disable the back button?
TIA!
Hey!
This is a little more dynamic method of doing it.
I needed the possibility to switch the back buttons function on and of depending on what the client where doing.
This is tested in Mozilla firefox and IE, the code is from a separate .js file
This will allow you to turn the back buttons function off with the functions ‘blockBackButton()’
And turn it on again with ‘resetBackButton()’
The script uses the get part of the url, but wont permanently change it becoause if the browser get a changed get part of the url, the script will reload the earlier page were the url was unchanged.
var ie = (window.navigator.appName == "Microsoft Internet Explorer") ? true : false;
function setEventByObject(object, event, func){
if (!ie){
object.addEventListener(event, func, false);
} else {
object.attachEvent("on" + event, func);
}
}
setEventByObject(win, "unload", exitme);
var block = "false";
function blockBackButton(){
block = "true";
}
function resetBackButton(){
block = "false";
}
function jumpforward(){
if(window.location.href.indexOf("&jumpforward")!=-1) {
history.forward();
}
}
jumpforward();
function exitme(){
if(block == "true") window.location.href += "&jumpforward";
}
Posts: 3
Time spent in forums: 48 m 44 sec
Reputation Power: 0
Kudos
Nicely done.
Thank you.
Quote:
Originally Posted by Linus sylvén
Hey!
This is a little more dynamic method of doing it.
I needed the possibility to switch the back buttons function on and of depending on what the client where doing.
This is tested in Mozilla firefox and IE, the code is from a separate .js file
This will allow you to turn the back buttons function off with the functions ‘blockBackButton()’
And turn it on again with ‘resetBackButton()’
The script uses the get part of the url, but wont permanently change it becoause if the browser get a changed get part of the url, the script will reload the earlier page were the url was unchanged.
var ie = (window.navigator.appName == "Microsoft Internet Explorer") ? true : false;
function setEventByObject(object, event, func){
if (!ie){
object.addEventListener(event, func, false);
} else {
object.attachEvent("on" + event, func);
}
}
setEventByObject(win, "unload", exitme);
var block = "false";
function blockBackButton(){
block = "true";
}
function resetBackButton(){
block = "false";
}
function jumpforward(){
if(window.location.href.indexOf("&jumpforward")!=-1) {
history.forward();
}
}
jumpforward();
function exitme(){
if(block == "true") window.location.href += "&jumpforward";
}
Posts: 1
Time spent in forums: 3 m 7 sec
Reputation Power: 0
About 'win' parameter
hi,
i gone through this code and tried it but i am getting one error that 'win' is undefined
so plz tell me what is that win
bye
Quote:
Originally Posted by Linus sylvén
Hey!
This is a little more dynamic method of doing it.
I needed the possibility to switch the back buttons function on and of depending on what the client where doing.
This is tested in Mozilla firefox and IE, the code is from a separate .js file
This will allow you to turn the back buttons function off with the functions ‘blockBackButton()’
And turn it on again with ‘resetBackButton()’
The script uses the get part of the url, but wont permanently change it becoause if the browser get a changed get part of the url, the script will reload the earlier page were the url was unchanged.
var ie = (window.navigator.appName == "Microsoft Internet Explorer") ? true : false;
function setEventByObject(object, event, func){
if (!ie){
object.addEventListener(event, func, false);
} else {
object.attachEvent("on" + event, func);
}
}
setEventByObject(win, "unload", exitme);
var block = "false";
function blockBackButton(){
block = "true";
}
function resetBackButton(){
block = "false";
}
function jumpforward(){
if(window.location.href.indexOf("&jumpforward")!=-1) {
history.forward();
}
}
jumpforward();
function exitme(){
if(block == "true") window.location.href += "&jumpforward";
}
Posts: 1,029
Time spent in forums: 1 Week 12 h 55 m 59 sec
Reputation Power: 15
First of all, this thread is over a year old, very slim chance you'll get a reply from that poster, seeing he hasn't posted much since then.
Second, whatever way you try to do this (disabling the back button), be ready for the fact it won't work on every browser. Every good browser has ways to disable javascript, and/or settings that determine how much power javascript has.
Do no try to limit a users power over his own computer. If I want to go back, for whatever reason, it's my good damn right. If doing that breaks your site, you should write better code, that checks for stuff like that.
__________________ This is my code. Is it not nifty?
"The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools."
---Douglas Adams