Click to See Complete Forum and Search --> : captureEvents
Webskater
04-17-2003, 04:55 AM
Can anyone tell me what is wrong with this - lifted straight from the DevEdge site:
<script language=javascript>
function callmyfunction()
{
document.write('You pressed a key');
}
document.captureEvents(Event.KEYPRESS);
document.onkeypress = callmyfunction();
</script>
When I put the above on a page to detect when someone clicks anywhere on the page I get a
'Event is undefined' message.
khalidali63
04-17-2003, 05:45 AM
Looks like this line
document.onkeypress = callmyfunction();
should be like this
document.onkeypress = callmyfunction;
Cheers
Khalid
Webskater
04-17-2003, 06:17 AM
Thanks for your answer Khalid. You are right of course, but it is not the problem. I did have the code the way you said but this does not work either. The page does not get to the point where the function is called. As the page loads the error message appears 'Event is undefined' and it refers to the line that says:
document.captureEvents(Event.KEYPRESS);
I copied the line above from the http://developer.netscape.com/docs/manuals/js/client/jsref/index.html site. I have to say if they cannot get things right who can. Does anyone else find javascript endlessly tiresome. Why doesn't it work? There's only two lines of code!
Also, and this is just by way of general interest, why is the event spelled 'onKeyPress' in the list of events on the above site but when it is used in code it changes to 'onkeypress'. Given javascript's case sensitivity - what's this all about?
khalidali63
04-17-2003, 06:25 AM
Oh I forgot to mention the code you are using will only work in NS browsers for ie you have to do it like this
if(!document.all || document.layers){
document.captureEvents(Event.KEYPRESS);
}
Khalid
khalidali63
04-17-2003, 06:50 AM
oh one more thing...your function would not work with NS4.7 to print the message and it will keep on try to load the document forever.
use the following crossbrowser pattern to use document.write
function callmyfunction(){
document.open();
document.write('You pressed a key');
document.close();
}
Cheers
Khalid
Webskater
04-17-2003, 06:58 AM
Thanks again Khalid.
As I am working on an IE only intranet application, that explains why it did not work. I am used to things working on IE but not on Netscape - not the other way round!
One last thing. What does the line
if(!document.all || document.layers){
actually mean?
khalidali63
04-17-2003, 07:00 AM
it means sequentially
if(not IE or its NS 4.7) then
process this statement
}
make sense?
the above will work if NS6+ browsers are used as well
Khalid
Webskater
04-17-2003, 07:09 AM
"if(not IE or its NS 4.7) then
process this statement
}
make sense?"
I know I am being thick here but ...
"if(not IE or its NS4.7)" confuses me. It is IE and when it (presumably) does not process the statement it works.
Are you saying that if its IE you don't need the line
document.captureEvents(Event.KEYPRESS);
at all? That (if its definitely IE) that just the line
document.onkeypress = callmyfunction
would do the trick.
Webskater
04-17-2003, 07:11 AM
Thanks again Khalid. Have answered my own last question. For IE you only need the one line.
Cheers.
khalidali63
04-17-2003, 07:15 AM
Yep ,you got that.
you are welcome
Cheers
Khalid
dsdsdsdsd
10-30-2004, 02:55 PM
I have just run into the same problem; I am glad to see this addressed;
thanks
dsdsdsdsd
OneThoughtJay
10-30-2004, 06:48 PM
Originally posted by khalidali63
Oh I forgot to mention the code you are using will only work in NS browsers for ie you have to do it like this
if(!document.all || document.layers){
document.captureEvents(Event.KEYPRESS);
}
Khalid
This pulls the trick indeed, but you should object detection when possible... this means:
if(document.captureEvents) document.captureEvents(Event.KEYPRESS);
:cool:
Hope this helps, goodluck,
Jay Kamminga