0

My jQuery code not working in IE 9 but work well in IE7, IE8, Chrome, FF.

This is my code:

$(document).keypress(function(){
        window.clearTimeout();
        window.setTimeout(function() {
            //Do something...
            return false;
        }, 800);
});
j08691
  • 204,283
  • 31
  • 260
  • 272
Sakata Gintoki
  • 1,817
  • 16
  • 23

3 Answers3

3

Try switching to keydown, which is different to the computer, but should achieve the same effect

From a tutorial:

In order to understand the difference between keydown and keypress, it is useful to understand the difference between a "character" and a "key". A "key" is a physical button on the computer's keyboard while a "character" is a symbol typed by pressing a button. In theory, the keydown and keyup events represent keys being pressed or released, while the keypress event represents a character being typed. The implementation of the theory is not same in all browsers.

bpeterson76
  • 12,918
  • 5
  • 49
  • 82
  • Thanks for your answer, i tried but it not working. I using the jquery code for affect like Google search input on home search page. When i typing anything words, some results is showing below. – Sakata Gintoki Mar 06 '13 at 21:10
1

Remove window.clearTimeout() or give it a parameter. IE9 incorrectly requires it.

$(document).keypress(function(){
    //window.clearTimeout();
    window.setTimeout(function() {
        alert("Worky!");
        return false;
    }, 800);
});

http://jsfiddle.net/Hztuv/1/

or

var timeout;
$(document).keypress(function(){
    window.clearTimeout(timeout);
    timeout = window.setTimeout(function() {
        alert("Worky!");
        return false;
    }, 800);
});
Kevin B
  • 94,570
  • 16
  • 163
  • 180
0

according to this is better to use keydown() instead of keypress() on IE

Community
  • 1
  • 1
jguilhermeam
  • 121
  • 1
  • 1
  • 10