PDA

View Full Version : Disable Enter Key JS as external script



martin7bcr
02-07-2008, 11:46 AM
1) Script Title:
Disable "Enter" key in Form script- By Nurul Fadilah
2) Script URL (on DD):
http://www.dynamicdrive.com/dynamicindex16/disableenter.htm
3) Describe problem:
I am using this script successfully and it is very useful. However I have found that it only works when the script is included in the html document. If I put the script in a separate text file with all the right headers etc as other javascripts, and call that from within the head section of the html page, it does not work. In order to use it on several forms it would be tidier to call it as an external file, anyone know why it does not work that way?

jscheuer1
02-07-2008, 02:47 PM
There is no reason why, if it works internally on a given page, that it shouldn't work externally on that same page. There are no:


all the right headers etc

for external javascript. This is all that should be in the external file:


/***********************************************
* Disable "Enter" key in Form script- By Nurul Fadilah(nurul@REMOVETHISvolmedia.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function handleEnter (field, event) {
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
if (keyCode == 13) {
var i;
for (i = 0; i < field.form.elements.length; i++)
if (field == field.form.elements[i])
break;
i = (i + 1) % field.form.elements.length;
field.form.elements[i].focus();
return false;
}
else
return true;
}

You would then call it from the head of your page:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script src="de.js" type="text/javascript">

/***********************************************
* Disable "Enter" key in Form script- By Nurul Fadilah(nurul@REMOVETHISvolmedia.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

</script>
</head>
<body>
<form>
<input type="text" onkeypress="return handleEnter(this, event)"><br>
<input type="text" onkeypress="return handleEnter(this, event)"><br>
<textarea>Some text</textarea>
</form>
</body>
</html>

where de.js is the path and filename to the text file.