PDA

View Full Version : Doing something when a key is pressed



bryanwebster
10-13-2008, 11:10 AM
Hi

We have a web blocking unit at the office, which shows a blocking web page when someone accesses porn etc.

The following code is on the page which either opens outlook to send an email or tries to access a url when a key is pressed. e for outlook or r to retry and access the page.



document.onkeydown=function(keyPressed){
if(keyPressed.which == 69) {
window.location="mailto:support@company.com?subject=%U %r %i"
return false;
} else if (keyPressed.which == 82) {
window.location="%U"
return false;
}
}


THis works great in firefox and safari but not IE. Can anyone point me in the right direction to what could be wrong?

note: %U %i %r are variables from our web blocking unit for Url, IP and reason why page was blocked.

bryanwebster
10-13-2008, 11:47 AM
Btw in IE I get the error message 'which' is null or not an object

Kor
10-13-2008, 01:23 PM
The function's argument is not the key pressed, is the event itself. IE and Mozilla have different models of capturing the events and different models to handle the key events:


<script type="text/javascript">
document.onkeydown=function(e){
!e?e=window.event:null;
var code=e.keyCode?e.keyCode:e.which;
if(code==69){window.location='mailto:support@company.com?subject=&#37;U %r %i';return false}
if(code==82){window.location='%U';return false}
}
</script>



e for outlook or r to retry and access the page.

Do you think all the people use Outlook by all means? I, for instance, use Thunderbird :D

bryanwebster
10-13-2008, 01:49 PM
Hey thanks for that! I am new to javascript (only just picked up a book yesterday) and I guess I was way off track?



Do you think all the people use Outlook by all means? I, for instance, use Thunderbird :D

LOL true, I try to avoid Outlook as well but all the office staff use it that is why I defaulted to saying Outlook instead of "their default email client"

But thanks again for the help it is much appreciated!