I need to emulate the scroll event on an iframe in my Firefox addon. I used the code below, but it doesn’t work and iframe scroll won’t move. And another question for me is how initUIEvent knows which direction scroll event must be dispatched for? (Horizontally or Vertically)
var windows = require("window-utils"),
selectedBrowser = windows.activeBrowserWindow.gBrowser.selectedBrowser,
contentWindow = selectedBrowser.contentWindow,
document = selectedBrowser.contentDocument;
obj = document.getElementById("scrollable_frame");
evt = document.createEvent("UIEvents");
evt.initUIEvent("scroll", true, true,
windows.activeBrowserWindow.gBrowser.selectedBrowser.contentWindow, 2);
obj.dispatchEvent(evt);
I think that your code dispatches the event just fine. But it doesn’t help solve your problem because it’s the frame scrolling that triggers the
scrollevent, not the other way round. If what you need is to scroll the frame then you can choose the direct route: usewindow.scrollByLinesorwindow.scrollByPages:To scroll horizontally you can use
window.scrollBy. This code will also trigger thescrollevent as a side-effect.