View Full Version : document wide events
forlamp
04-07-2004, 11:29 AM
hello, i'm trying to catch a 'keyup' event document wide, now if i use
document.onkeyup = somefunc;
that works in IE, but not in netscape... does netscape and other browsers even support document wide events? I'd hope so...
A few other questions, does Mozilla use the same Javascript engine as Netscape? or FireFox? ... anyway
I also need to figure out the best way to determine which browser the client is using.
blah = (window.layers) ? event.IEsomething : event.NS; ?
i'd appreciate any help..
-fl
agent002
04-07-2004, 12:04 PM
Yes, as Mozilla, Firefox and Netscape use the same HTML rendering engine, they also use the same JavaScript engine. They also support document.onkeyup just as well, but you are pretty obviously having trouble getting the actual event values, as the way of getting them is different. With IE, there is an object called event inside the function if it's called at an event, like:
<script type="text/javascript">
function keyUpEvent(){
alert('Event object: '+event);
}
</script>
.....
<body onkeyup="keyUpEvent();">
However, with Mozilla, the event object only exists at the onkeyup="" and you will need to pass it to the function.
<script type="text/javascript">
function keyUpEvent(e){
alert('Event object: '+e);
}
</script>
.....
<body onkeyup="keyUpEvent(event);">
When calling the function from document.onkeyup, the event is passed as the first argument automatically:
<script type="text/javascript">
function keyUpEvent(e){
alert('Event object: '+e);
}
document.onkeyup = keyUpEvent;
</script>
To make the thing cross browser compatible, use:
<script type="text/javascript">
function keyUpEvent(e){
var eventObj = e? e : event;
alert('Event object: '+eventObj);
}
</script>
That works in both browsers already. But the property names are also different, e.g. when IE wants .keyCode, Mozilla wants .which. You can use something like:
var kcode = (e && e.which)? e.which : event.keyCode;
Here (http://www.wdvl.com/Authoring/JavaScript/Events/) is a resource for you. :)
forlamp
04-07-2004, 12:24 PM
Well, this is what i have
document.onkeyup = getk;
function getk(e)
{
var k = (e.keyCode) ? e.keyCode : e.which;
alert(k);
}
this is what i used, and it doesnt seem to work.. it works in IE just fine however, im not sure if it's the 'document.onkeyup' part.. notice that isn't in a function, it is in the header of the html file, in attempts to make the event global, no matter what element they have selected in the page..
agent002
04-07-2004, 12:26 PM
Try:
function getk(e)
{
var k = (e && e.which) ? e.which : event.keyCode;
alert(k);
}
:)
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.