Reputation: 3738
So, I need use this event so I can navigate trought blog posts. I use the 'J' key to go to the previous post and the 'K' key to go to the next post. My problem is that the event works on the first try but then doesn't work anymore. When i restart the browser it works if i press J or K and it redirects me to the previous/next post. But then if I press again it does nothing.
Sorry if I can't explain it exactly enought and thanks for helping.
$(document).keyup(function (event) {
if (event.keyCode == 74) {
var left_link = $('#nav-left a').attr('href');
alert(left_link);
if(typeof left_link !== 'undefined' && left_link !== false)
window.location = left_link;
}
else if (event.keyCode == 75) {
var right_link = $('#nav-right a').attr('href');
alert(right_link);
if(typeof right_link !== 'undefined' && right_link !== false)
window.location = right_link;
}
});
Even if I don't do the redirect and only those alerts it doesn't work.
Upvotes: 3
Views: 2673
Reputation: 3738
There was a problem with the script loading, I applied a delay of 1 second and now it works just fine.
Thanks anyway.
Upvotes: 3
Reputation: 664185
After you've changed the location, a new document is loaded. A document without a listener for keyup events.
Upvotes: 3