Joe W
Joe W

Reputation: 1008

jQuery keyup triggered when tabbing to page?

I'm trying to create a live search on my site using jQuery and ajax.

However, if the search box has focus, and I move to another browser tab, jQuery triggers the keyup() function when I come back to the search page.

I've mocked this up in JSfiddle

Steps to reproduce:

  1. Open the JSfiddle and any other browser tab
  2. Give the text box focus
  3. Move to another browser tab
  4. Go back to the original page (the JSfiddle)

Expected/intended: keyup() isn't triggered

What happens: keyup() triggered, as shown by timestamp

I don't know much about Javascript and I've read the jQuery documentation, but I can't come up with an answer.

Is this happening because I'm doing something wrong? Is it a Javascript/jQuery or browser issue? (Using Chrome) How might I prevent it happening?

Thanks! Be gentle.

UPDATE

It appears it is only triggered if using Ctrl+Tab, rather than just clicking between tabs. Can be replicated by clicking away from the page, but Ctrl+Shift+Tab to get back to search page.

I would suppose the solution is as mentioned below, to return keyCode 9 as false, but will leave it open for other suggestions.

Upvotes: 0

Views: 536

Answers (1)

ahren
ahren

Reputation: 16959

I'm also using chrome (on win7). Cannot reproduce. One solution for you though, would be to check which keycode (e.keyCode) is being generated, and return false if it's always the same one. For ex, if the keyCode is 37:

$('#search').keyup(function(e) {
    if(e.keyCode == 37) return false;
    $('#test').append('<li>'+e.timeStamp+'</li>');
});
​

Upvotes: 1

Related Questions