Forum Moderators: open

Message Too Old, No Replies

textboxes, javascript and the [enter] key

         

j4mes

1:43 pm on Apr 4, 2004 (gmt 0)

10+ Year Member



I've got an odd problem - probably nothing new but not something I'd come across before - using a form button and onClick to change location.href works fine, but pressing [enter] doesn't. (This is for a simple whois btw).

Here's the code to see what I mean:


<html>
<head>
<script>
function checkit() {
location.href=("http://195.66.240.211/cgi-bin/whois.cgi?query=" + document.check.domain.value);
}
</script>
</head>
<body>
<form name="check">
<input type="text" name="domain">
<input type="button" value="Check It" onClick="checkit()">
</form>
</body>
</html>

(The IP is NIC UK's whois server)

Sloppy I know, but you get the point. Anyway, type in a domain to whois and click the button and it works just fine. However, pressing [enter] just appends "?domain=DOMAIN.EXTENSION" to the current href. Has anyone else come across this, and is it easily fixable?

j4.

My first post, Woo!

PCInk

1:59 pm on Apr 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You do not have a sumbit button (input type='submit') and you do not have a form action (form action='filename.cgi').

PCInk

2:02 pm on Apr 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Forget javascript - not everybody has it anyway:

<form name="check" action="http://195.66.240.211/cgi-bin/whois.cgi">
<input type="text" name="query">
<input type="submit" value="Check It">
</form>

The action has the filename, the text input has the variable name(s) required as parameters and the submit tells the browser to submit the form when clicked (even if javascript is off).

Really, you should have a method="..." where ... is Post or Get, but most browsers do not need this.

j4mes

2:45 pm on Apr 4, 2004 (gmt 0)

10+ Year Member



Fantastic, thanks PCInk.

Yeah, I generally try to avoid javascript where possible, but this one's just a personal tool at the minute.

Rambo Tribble

5:51 pm on Apr 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To elaborate on why you had different behaviour between the button and the enter key; the button called the function with no onsubmit event, the enter created the onsubmit event which utilized the form action, method, etc.