Firefox/Mozilla and window.event.keyCode

P

Perttu Pulkkinen

I have different functions that receive window.event as parameter. Functions
are used like this:

<input type="text" id="x"
onkeypress="return onKeyCurrencyCheck(ev, 'x')"
onblur ="onBlurCurrencyCheck(event, 'x')"">

Works very well with IE, but window.event and maybe window.event.keycode too
seems to be missing form firefox. Is there a workaround for this?

Perttu Pulkkinen



function toCurrency(val)
{
if(val <0)
{ euros = Math.ceil(val); cents = Math.abs(Math.ceil(val*100) -
euros*100); }
else
{ euros = Math.floor(val); cents = Math.floor(val*100) - euros*100; }
if(cents <10) cents = "0" + cents;
return euros +"." + cents;
}
/**********************************************************/
function onKeyCurrencyCheck(ev, eleid)
{
ret = false;
ele = getElement(eleid);
if((48 <= ev.keyCode) && (ev.keyCode <= 57)) { ret = true;}
else if(ev.keyCode == 8 || ev.keyCode == 46 ) {ret = true;}
else if(ev.keyCode == 44) { ev.keyCode = 46; ret =
true;}
else if(ev.keyCode == 13) //enter
{
parsedvalue = parseFloat(ele.value);
if(isNaN(parsedvalue) == false)
{ ele.value = toCurrency(parsedvalue); }
else
{ ele.value ="0.00";}
}
return ret;
}
/**********************************************************/
function onBlurCurrencyCheck(ev, eleid)
{
ele = getElement(eleid);
parsedvalue = parseFloat(ele.value);
if(isNaN(parsedvalue) == false)
{ ele.value = toCurrency(parsedvalue); }
else
{ ele.value ="0.00"; }
}
 
D

Daniel Kirsch

Perttu said:
I have different functions that receive window.event as parameter. Functions
are used like this:

<input type="text" id="x"
onkeypress="return onKeyCurrencyCheck(ev, 'x')"
onblur ="onBlurCurrencyCheck(event, 'x')"">

Works very well with IE, but window.event and maybe window.event.keycode too
seems to be missing form firefox. Is there a workaround for this?

Firefox doesn't use the global window.event but the passes an event
object to the handler. However if you use just "event" it will work as
this will be the event object within your handler.

onkeypress="return onKeyCurrencyCheck(event, 'x')"

keyCode is also know by firefox, but if you use keypress you may check
for charCode.
IIRC it also supports the old which property (event.which).

Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top