Firefox follows the DOM standard and passes an event object to each event listener, however has the global event: windows.event.
Problem:
I wanted to fire a piece of javascript after the user keyed some text and pressed enter or clicked a button. The skeleton code is below:
<input id=”NewsletterEmail” value=”Email here” type=”text”
onfocus=”javascript:this.value = ”;” class=”WiderLoginTextBox” />
<input value=”OK” type=”button”>
Solution:
Firstly to handle the click of the button I added a call to a javascript function to the button’s onclick event:
onclick=”javascript:return SubscribeToNewsletter(event);”
and the same call on the text box’s onkeypress event:
onkeypress=”javascript:return SubscribeToNewsletter(event);”
The javascript function code is below:
function SubscribeToNewsletter(e)
{
var keycode;
var el;
if (!e)
{
// ie does not pass the event as a parameter!
e = window.event;
el = e.srcElement; // ie uses the window.event.srcElement property
}
else
el = e.target; // non-ie uses the window.event.target property
keycode = e.which;
if (keycode == 13 || el.type == “button”) // enter or button clicked?
{
RegisterForNewsletter(); // call another piece of javascript to do the work…
return false;
}
else
return true;
}