admin 管理员组

文章数量: 1086019

It used to work but now I get a:

window.event is undefined

From this simple code that used to work:

function checkKey() {   
    if (window.event.keyCode != 9) {    
        document.actionForm.saveStatus.value = "Not saved";
    }
}

Why can't I use window.event anymore?

It used to work but now I get a:

window.event is undefined

From this simple code that used to work:

function checkKey() {   
    if (window.event.keyCode != 9) {    
        document.actionForm.saveStatus.value = "Not saved";
    }
}

Why can't I use window.event anymore?

Share Improve this question asked Oct 29, 2012 at 9:45 Niklas RosencrantzNiklas Rosencrantz 26.7k77 gold badges243 silver badges444 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

window.event is a proprietary Microsoftism.

The standard way to access data about an event is via the first argument of the event handler function.

function checkKey(e) {
  var evt = e || window.event,
      keyPressed = evt.which || evt.keyCode;
  if (keyPressed  != 9) {    
    document.actionForm.saveStatus.value = "Not saved";
}

You can standardize the check like so:

function checkKey(e) {
    var evt = e || window.event;
    if (evt.keyCode != 9) {    
        document.actionForm.saveStatus.value = "Not saved";
    }
}

本文标签: javascriptWhy is windowevent not workingStack Overflow