Reputation: 1008
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:
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
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