Disable back for javascript

#1

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 }">

0 Likes

#2

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>

0 Likes

#3

disable the back button?

make sure you also disable Alt-leftarrow, then

0 Likes

#4

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.

0 Likes

#5

<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>

0 Likes

#6

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

Thanks!

0 Likes

#7

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!

0 Likes

#8

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.

0 Likes

#9

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!

0 Likes

#10

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?

0 Likes

#11

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

Nope.

0 Likes

#12

   <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.

0 Likes

#13

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

0 Likes

#14

bounce

0 Likes

#15

please don't bump threads

0 Likes

#16

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

0 Likes

#17

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.

0 Likes

#18

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>
 
0 Likes

#19

Hah! It works! Danke schoen.

0 Likes

#20

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

0 Likes