Disable back for javascript

I have a form in which I disable the backspace key from going back a page if typed.

It works in IE I can disable the backspace key from going back but not in mozilla. Does anyone know why this does not work in mozilla?

 
<body onkeydown="if (event.keyCode==8) {event.keyCode=0; return event.keyCode }">

Two reasons: the document object is the place to (globally) handle keyboard events, and you need to use the keypress event to be able to cancel them.


<script type="text/javascript">

if (typeof window.event != 'undefined')
	document.onkeydown = function()
	{
		return (event.keyCode != 8);
	}
else
	document.onkeypress = function(e)
	{
		return (e.keyCode != 8);
	}

</script>

disable the back button?

make sure you also disable Alt-leftarrow, then

sorry i forgot to mention there are text fields so I need to be able to backspace. the javascript code does not allow the backspace at least for me in ie.

I may need to just use sessions or cookies to save the data instead of javascript i was just looking for a quick fix that worked in ie and mozilla.


<script type="text/javascript">

if (typeof window.event != 'undefined')
	document.onkeydown = function()
	{
		if (event.srcElement.tagName.toUpperCase() != 'INPUT')
			return (event.keyCode != 8);
	}
else
	document.onkeypress = function(e)
	{
		if (e.target.nodeName.toUpperCase() != 'INPUT')
			return (e.keyCode != 8);
	}

</script>

Wow that was quick. It works perfect, that is exactly what I needed.

Thanks!

Hi!!!
My name es Elizabeth.

I was trying to disable the backspace key. I found this site and I tried to understand your code, but there is something i can’t. In these lines: document.onkeydown = function()
document.onkeypress = function(e)
what does function() mean?

I hope you can help me.
Thanks!

What adios has done is use a function literal to define an unnamed function. JavaScript has three ways to define functions: 1) the function statement, the most common technique which creates a function which can be called by name 2) the use of the Function() constructor, which is largely supplanted by the third way 3) the function literal. What function literals allow is the creation of functions “on the fly”.

In this example, adios is assigning his function code to existing keyboard events, so the functons do not require calls by name.

Hi all!

I’m trying to disable some hot keys on webform by javascript but i can’t disable altKey combination like alt+Home, alt+LeftArrow…
Can u give me the solution!?

Thanks!

I hope the person who originally wrote this is still around. I tried this script and it works great except for one serious flaw. All input elements will return a legit backspace and not merely text fields. The textarea element also needs to be included. How would I go about making those changes, being a JS n00b and all?

I hope the person who originally wrote this is still around.

Nope.


   <script type="text/javascript">
   
   if (typeof window.event != 'undefined')
   	document.onkeydown = function()
   	{
   		var test_var=event.srcElement.tagName.toUpperCase();
   		if (test_var != 'INPUT' && test_var != 'TEXTAREA')
   			return (event.keyCode != 8);
   	}
   else
   	document.onkeypress = function(e)
   	{
  		var test_var=e.target.nodeName.toUpperCase();
   		if (test_var != 'INPUT' && test_var != 'TEXTAREA')
   			return (e.keyCode != 8);
   	}
   
   </script>
   

Note: Adios’ script does not work on Konqueror, so it is unlikely to work on Safari, as well. Nor does it work in Opera.

Questions are always so much clearer in my head. :blush:

I meant, I need to turn off the backspace for radio buttons, checkboxes, and buttons also. If a user hits backsapce on one of those elements, they’ll lose all the data they’ve already entered. It should only work in <input type=text> and the textarea element.

Alternative browers aren’t a problem. We have a captive audience and can dictate the browsers they use (it goes against my philosophy, but it’s not my choice either).

Thanks

bounce

please don’t bump threads

I’m afraid I’m not following. I’m not seeing this behavior on my systems.

My environment is IE6 on Win2K. It’s for an internal corporate app, so I can’t change that. Usingt the provided script, if a user hits backspace on any input element, the default action will occur. In a text field that’s to remove characters, but on a check box, radio button, and buttons, backsapce will return the user to a previous page, thus causing them to lose any unsaved data in the form.

I believe the following will do what you want on IE and Firefox. It is partially effective on Opera; it works with input fields but not when the page at large has focus, as Opera doesn’t seem to support intercepting the larger context event (it’s possible that using event listeners could offer relief, if you want to pursue it).

An easier solution comes to mind, however: since Konqueror doesn’t support the backspace key as back button, just switch your users to desktop Linux and KDE!


 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <html>
 <head>
 <title>Prevent Backspace</title>
 <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
 <style type="text/css">
 </style>
 <script type="text/javascript">
 if (typeof window.event == 'undefined'){
   document.onkeypress = function(e){
 	var test_var=e.target.nodeName.toUpperCase();
 	if (e.target.type) var test_type=e.target.type.toUpperCase();
 	if ((test_var == 'INPUT' && test_type == 'TEXT') || test_var == 'TEXTAREA'){
 	  return e.keyCode;
 	}else if (e.keyCode == 8){
 	  e.preventDefault();
 	}
   }
 }else{
   document.onkeydown = function(){
 	var test_var=event.srcElement.tagName.toUpperCase();
 	if (event.srcElement.type) var test_type=event.srcElement.type.toUpperCase();
 	if ((test_var == 'INPUT' && test_type == 'TEXT') || test_var == 'TEXTAREA'){
 	  return event.keyCode;
 	}else if (event.keyCode == 8){
 	  event.returnValue=false;
 	}
   }
 }
 </script>
 </head>
 <body>
   <form action="">
 	<p>
 	  <input type="checkbox" />
 	  <input type="checkbox" />
 	  <input type="checkbox" />
 	</p>
 	<p>
 	  <input type="text" />
 	</p>
 	<p>
 	  <textarea></textarea>
 	</p>
   </form>
 </body>
 </html>
 

Hah! It works! Danke schoen.

Hii

Am Akram. I want to disable Back and Forward options found in Right click on Webpage. Javascript can do the disable of Right Click, but really i dont need that. Juz i want to disable that two options or events.

More than that, Is that possible to stop this following Key Combination Events?

  1. Alt+Back Arrow
  2. Alt+Forward Arrow

plz help me on this, because these strokes make my JSP pages expire. I dont want to allow my user to navigate through History.

Thanks in Advance,

AKRAM K KANNAN