views:

36

answers:

1

I am trying to trigger a click event on an element on a page from within a Firefox sandbox. I have tried using jQuery's .click() as well as doing:

var evt = document.createEvent("HTMLEvents");
evt.initEvent("click", true, false );
toClick[0].dispatchEvent(evt);

Has anyone been able to trigger a click event on a page in the browser through a sandbox? I can get the DOM element fine, but triggering the event is a different story.

+1  A: 

You have to create the event on the right document:

var evt = pageDocument.createEvent("HTMLEvents");
evt.initEvent("click", true, false );
toClick[0].dispatchEvent(evt);

The true means the event "bubbles" and the false means the event cannot be cancelled. From https://developer.mozilla.org/en/DOM/event.initEvent

JAM
I edited your answer to include a description of what the boolean params to initEvent mean, plus a link. Hope you don't mind!
MatrixFrog