How to call button click event in another page's load event?

Firefox Add-ons: How do I call a function from inside an injected HTML button

  • I have a code module in my Firefox extension that I am trying to call from an input button I inject onto the HTML page. The problem is it doesn't allow me to call the function from my event listener. Tried many different approaches but I thought this was best one and did not work. My code: onPageLoad: function(aEvent) { Components.utils.import("chrome://my_ext/content/writeFile.jsm", my_ext); //alert(foo()); - foo() is an function in the above code module I import var doc = aEvent.originalTarget; // doc is document that triggered "onload" event var mEvent = doc.createEvent("Events"); mEvent.initEvent("mClick", true, true); var fblike = doc.getElementById("LikePluginPagelet"); var button = doc.createElement("input"); button.setAttribute("type", "button"); button.setAttribute("value", "My Button"); button.addEventListener("click", function() { button.dispatchEvent(mEvent); }, false); fblike.appendChild(button, fblike); doc.addEventListener("mClick", function() {alert(foo()); }, false); }, My writeFile.jsm: var EXPORTED_SYMBOLS = ["foo"]; function foo() { return "foo test"; }

  • Answer:

    // Note second argument is not necessary.// Default value is correct.Components.utils.import("chrome://my_ext/content/writeFile.jsm");// You might want to just call foo() here as a test, to// make sure it gets imported correctly.var elm = document.createElement("button");elm.addEventListener("click", foo);doc.body.appendChild(elm);

Aaron Boodman at Quora Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.