views:

167

answers:

3

I'm using Ctrl+Left / Ctrl+Right in a GreaseMonkey script as a hotkey to turn back / forward pages. It seems to works fine, but I want to disable this behavior if I'm in a text edit area. I'm trying to use document.activeElement to get the page active element and test if it's an editable area, but it always returns "undefined".

+2  A: 

document.activeElement works for me in FF3 but the following also works

(function() {

var myActiveElement;
document.onkeypress = function(event) {
    if ((myActiveElement || document.activeElement || {}).tagName != 'INPUT')
        // do your magic
};
if (!document.activeElement) {
    var elements = document.getElementsByTagName('input');
    for(var i=0; i<elements.length; i++) {
        elements[i].addEventListener('focus',function() {
            myActiveElement = this;
        },false);
        elements[i].addEventListener('blur',function() {
            myActiveElement = null;
        },false);
    }
}

})();
Jimmy
A: 

@Jimmy:

I'm using FF 2.0.0.16 until my extensions runs in FF3. It looks like activeElement doesn't work in FF2, but the focus/blur events did the trick, thanks

PabloG
A: 

element.activeElement is part of HTML5 spec but is not supported by most browsers. It was first introduced by IE.

thesmart