Forum Moderators: open

Message Too Old, No Replies

i need help script not working

         

MrGecko

12:13 pm on Aug 15, 2007 (gmt 0)

10+ Year Member



can some one tell me why this script is not working
<input id="message" type="text" size="60" onKeyPress="if (event.keyCode == 28) {send()}">
i think 28 = return it this script was working i type something in the text box and when i hit return or enter it should run the function send but it dosenot

[edited by: MrGecko at 12:14 pm (utc) on Aug. 15, 2007]

Fotiman

4:41 pm on Aug 15, 2007 (gmt 0)

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



First, I believe the keycode for Return/Enter is actually 13.

But more importantly, your approach is flawed in that the global 'event' object only exists for IE, so this won't work for users of other browsers (Firefox, Opera, Safari).

What you might do instead is use a JavaScript Library like the Yahoo UI (aka YUI) Library to capture the event. The YUI Event Utility is robust and will work across browsers. Below is how you might accomplish your goal with the YUI. Note also the use of unobtrusive JavaScript (instead of using the onKeyPress attribute, attach your script handlers from within your JavaScript... it leaves your markup cleaner and easier to maintain):


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset:utf-8">
<title></title>
<script type="text/javascript" src="http://yui.yahooapis.com/2.2.2/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript">
function send() {
alert('sending...');
}
YAHOO.util.Event.on(window, 'load', function(e) {
// Attach event handlers
YAHOO.util.Event.on('message', 'keypress', function(e) {
if (YAHOO.util.Event.getCharCode(e) == 13) {
send();
}
});
});
</script>
</head>
<body>
<form action="">
<div>
<input id="message" type="text" size="60" >
</div>
</body>
</html>

MrGecko

5:35 pm on Aug 15, 2007 (gmt 0)

10+ Year Member



hey your right for 13 being the code for return and enter but your wrong about event only working for ie event dose not work with ie

Fotiman

4:00 pm on Aug 16, 2007 (gmt 0)

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



No, actually I'm right about both of those. :) IE uses a global window.event object, whereas mozilla based browsers (Firefox) pass the event object as a parameter to the event handler.

Here's a sample script that proves it.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset:utf-8">
<title></title>
<script type="text/javascript">
window.onload = function() {
var foo = document.getElementById('foo');
foo.onclick = function(e) {
alert('e = ' + e); // Works for Firefox
alert('event = ' + event); // Works for IE
}
}
</script>
</head>
<body>
<a href="#" id="foo">Click me</a>
</body>
</html>

Also worth noting that these objects are slightly different. For example, in IE the event has a keyCode and srcElement properties, but in Firefox the equivalent properties are charCode and target. Rather than dealing with all that, the Yahoo Event Utility gives you a nice API and it handles all of the browser specific stuff behind the scenes so you don't need to worry about it.