Results 1 to 2 of 2

Thread: Disable Enter Key JS as external script

  1. #1
    Join Date
    Feb 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Disable Enter Key JS as external script

    1) Script Title:
    Disable "Enter" key in Form script- By Nurul Fadilah
    2) Script URL (on DD):
    http://www.dynamicdrive.com/dynamici...sableenter.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?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    /***********************************************
    * 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:

    Code:
    <!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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •