Disable browser back-button using javascript

I don’t know that you could, and I certainly wouldn’t recommend doing so…I know that if it were to happen to me, I’d be more than a little ticked off.


Chris Bowyer – chris@mycoding.com
MyCoding.com: Visit for Launch Notification!
DomainMailings.com: Who Says All The Good Ones Are Taken?
MovieForums.com: Talk About Your Favorite Flicks!

how could I disable the browser’s back button using javascript?

sam

Ahhhhh. Once again this topic has come up, why on earth do you want to disable the back button? It is pointless and would make me and the fast majority of surfers exit your site straight away.


Karl Austin
KDA Web Services
“Everyone has a photographic memory. Some just don’t have film.”

About the only way you can do this is to open a new window using window.open() (specifying that the browser history should not be copied to the new window) and then close the existing one.

As others have already pointed out, however, there doesn’t seem to be any good reason to do this.


-Kevin Yank.
http://www.SitePoint.com/
Helping Small Business Grow Online!

Hi,

If you are concerned that someone will resubmit a form or be out of sequence if they use the back button, you could reset the form elements upon loading the document. Or you can check if history.go(1) is equal to a known document. In which case you can put up an alert. But, kill the button? Nope.
Vinny


my site:GrassBlade: cut&paste javascript
moderator at:The JavaScript Place Forums
Javascript City

I DON’T NEED ADVICE!!

ALL THAT I ASK IS IF THERE IS A WAY TO DISABLE THE BACK BUTTON…

i’m not asking for too much, right?

Sam

so no one knows how to do this huh?

sam

Chill out…we’re simply making it clear that there so far, between the people who have posted, there’s no conceivable appropriate use for this. The fact that you seem so upset with this makes me question why you’re even inquiring…

And of course, as Kevin said, there’s only one way he knows of, and he’s a major guru in this sort of area.

This may sound rude, but if I did know how to do this, I’d probably request more info before telling you how…this could be used in less-than-moral ways for adult sites and such…


Chris Bowyer – chris@mycoding.com
MyCoding.com: Visit for Launch Notification!
DomainMailings.com: Who Says All The Good Ones Are Taken?
MovieForums.com: Talk About Your Favorite Flicks!

i wasn’t mad, just that i wanted an answer.

it’s not going to be used for a porn site or any public domain, in fact it will be used for an e-business application that will be hosted on my company’s intranet.

no ethics in question, just a simple back button…

thanks anyway.

Sam

Hi Sam,

Again, I don’t know of any method per se, but why is it important and maybe i can point you in a different but equally effective direction
Vinny


my site:GrassBlade: cut&paste javascript
moderator at:The JavaScript Place Forums
Javascript City

Although I’m not in the habit of repeating myself…

<BLOCKQUOTE><font size=“1” face=“Verdana, Arial”>quote:</font><HR>About the only way you can do this is to open a new window using window.open() (specifying that the browser history should not be copied to the new window) and then close the existing one.<HR></BLOCKQUOTE>

If that solution isn’t acceptable to you, you’ll have to do without. For security reasons, JavaScript provides no way to do what you want. Sorry.


-Kevin Yank.
http://www.SitePoint.com/
Helping Small Business Grow Online!

It can be done in 4+ browsers but it doesn’t really disable the back button, just erases the data behind it to an extant.

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)

This will replace the history with the current link every time a link is clicked
<BLOCKQUOTE><font size=“1” face=“Verdana, Arial”>code:</font><HR><pre>
<SCRIPT>
function getTag(el,str) {
while ((el!=null) && (str.indexOf(el.tagName + “:”)<0))
el = el.parentElement
return el
}

function navigateTo(sURL,target) {
if ((target == ‘_self’) | | (target==“”)) {
window.location.replace(sURL);
return false;
}
if (target == ‘_top’) {
top.window.location.replace(sURL);
return false
}
if (target ==‘_parent’) {
parent.window.location.replace(sURL);
return false;
}
if (target == ‘_blank’ | | parent.frames.length < 1) {
window.open(sURL, target);
return false;
}
else {
if (parent.frames[target])
parent.frames[target].location.replace(sURL);
else
window.open(sURL, target);
return false;
}
}

function checkIEClick() {
var el = getTag(event.srcElement,“A:AREA:”)
if ((el!=null) && ((el.tagName==“A”) | | (el.tagName==“AREA”))) {
event.returnValue = false
navigateTo(el.href,String(el.target).toLowerCase())
}
}

function checkNSClick(ev) {
if (ev.target.href) {
navigateTo(ev.target.href,String(ev.target).toLowerCase())
return false
}

}

if ((document.all) | | (document.layers))
if (document.layers) {
document.captureEvents(Event.CLICK)
document.onclick = checkNSClick
}
else
document.onclick = checkIEClick
</SCRIPT>



A little slick redirection:
<BLOCKQUOTE><font size="1" face="Verdana, Arial">code:</font><HR><pre>
<META HTTP-EQUIV="Refresh" CONTENT="1; <A HREF="http://www.insideDHTML.com/home.asp">" TARGET=_blank>http://www.insideDHTML.com/home.asp"></A> 
<SCRIPT>
<!--
  var version = parseInt(navigator.appVersion)
  // replace is supported
  if (version>=4 | | window.location.replace)
    window.location.replace("newPage.htm")
  else
    window.location.href = "newPage.htm"
// -->
</SCRIPT>
The document, <A HREF="newPage.htm">Scott's Home</A> has moved.

The above code was found at Site Experts. I hope it is useful.


Wayne Luke - Sitepoint Forums Administrator
Digital Magician Magazine - MetaQuark Creations (Coming Soon)
sitepoint@digitalmagician.com

[This message has been edited by wluke (edited August 03, 2000).]

Before wasting your time, you should be aware that the above code only replaces the current document with the new document to be loaded by a link. It does not affect the rest of the browser history. There is no way to remove pages that were present in the browser history before the current page was loaded.

In effect, you can only use the above code to prevent people from reaching other pages on your site using the back button, and you cannot use this method to perform form submissions.

-Kev.


-Kevin Yank.
http://www.SitePoint.com/
Helping Small Business Grow Online!

[This message has been edited by kyank (edited August 03, 2000).]

wluke,

you’re the bomb.

FYI - we’re using silverstream (application server) as our development environment and it writes it’s own javascript.

thanks!

Sam

I keep on getting a close confirmation box when my script closes the window…please help! (NE4.5)

sam

Don’t tell him <grin>
In terms of usability it is the pits!


www.gib-online.com
The Rock of Gibraltar on the WEB
www.urban-studios.com
…for all your IT solutions

why does everyone hate me? i’m just trying to figure this out. i’m using it at work for a test that everyone has to take and it’s not working.

so how do i get this confirmation out of the way?

sam

c’mon i’m a cool guy!