简体   繁体   中英

function expression/event detection not working in ie<=9 or Safari

I've been searching for an answer to this for about a day now, I have a function that just won't work in IE9 or below or Safari. I think it's because it's triggered by a keyboard event. It also passes the event to the function so onkeyup the function can check the value and act accordingly.

I was wonder if anyone could enlighten me as to what the problem is, the function is as follows:

window.onkeyup = function(e) { //This doesn't seem to work in the above browsers

   var key = e.keyCode ? e.keyCode : e.which; //But neither does this

   if (key == 83 && array.length>=0) {

        // Log answer as 'S'

   }else if (key == 68 && array.length>=0) {

        //Log answer as 'D'

   }

}   

Any help would be appreciated, thank you.

Could be because array is not defined in the code, at least in the part of code is visible here. Try invert the condition, maybe will works then.

(e.which) ? e.which : e.keyCode

Other idea is use the code below before use e in the previous comparisons:

 if (window.event){
   e = window.event.keyCode;}
   else if (event.charCode) {
    e = event.charCode;}
   else if (event.which){
   e = event.which; } 
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM