1

I want to simulate a scroll down with the "Page Down" button in a page. I have the call to a function every 2 seconds, but dont know how to connect it with the keyboard buttons (how to simulate a keyboard button press?).

var interval = null;

jQuery(function(){
  interval = setInterval(callFunc, 2000);
});

function callFunc(){
  jQuery('.link1, .link2, .link3').trigger('click');
}

This seems to work, meaning that when inserted in the console it call the callFunc function every 500ms, but I cant fix the part inside the function to simulate the button press. (keycode for "page down" button is 34)

var interval = null;

$(function(){
  interval = setInterval(callFunc, 500);
});

function callFunc(){
  var event = $.Event('keypress');
  event.which = 34; 
  event.keyCode = 34; 
  $(this).trigger(event); 
}   

Anyone?

Machavity
  • 30,841
  • 27
  • 92
  • 100
Rrezarta Muja
  • 21
  • 1
  • 4

1 Answers1

1

Finally managed to achieve what I wanted. Instead of simulating a keypress of "Page down", which for some unknown reason for me didn't work (I think I got the keycode wrong), I simulate a scrolldown:

var interval = null;

$(function(){
  interval = setInterval(callFunc, 500);
});

function callFunc(){
  var scroll = $(window).scrollTop();
  var scrollto = scroll + 500;
  $("html, body").animate({scrollTop: scrollto});
}

This is a way to scroll down automatically in any page and trigger the infinite scroller of the page. My example is for retrieving a big list of facebook page likers:

https://www.facebook.com/search/[PADE_ID]/likers

Rrezarta Muja
  • 21
  • 1
  • 4
  • You didn't simulate wrong, is just the way it works.It shouldn't but it is. If you try the same with an input and try to set keyCode 65 which represent "a" it wont appear into the input the a. I think is bad function of javascript in general... – titusfx Jan 29 '17 at 12:20