How to let a sidebar execute javascript code from the main document?

I'm working on a firefox sidebar that loads a webpage in the main document. Now i'd like to call a javascript function from the main document in response of a pushed button on the sidebar. How can the sidebar cause the main document to execute one of its javascript functions? So far, I'm having a variable containing the javascript code, but how can it be executed? (in the context of the loaded website?)

Answers


Dr.Molle is correct with his answer but I always love working examples.

console.log("Opening new page at http://example.com");
page = window.open('http://example.com');
console.log(page);
console.log("calling alert on opened page");
page.alert("I opened and the previous windows JS called me!!!!");
console.log("called alert");

Example page => [Removed. See update below]

Results:

Note: I had to allow popups for the demo code to work but that is another issue for another SO question. ; )

UPDATE:

So doing

page.alert()

worked because it didn't have to wait for the page to finish loading for it to work. In order to be able to call a function is a script on the loaded page you have to ensure that the script has finished loading. To do illustrate this I changed my example.

Example page => http://mikegrace.s3.amazonaws.com/forums/stack-overflow/example-call-function-in-opened-page.html

The JS looks like this

page = window.open('http://mikegrace.s3.amazonaws.com/forums/stack-overflow/example-the-other-page.html');

var initOtherPage = function() {
  try {
    page.showMeTheMoney();
    console.log("It worked!");
  } catch (e) {
    console.log("failed to call function on other page because: "+e);
    setTimeout(function() {
      console.log("trying again")
      initOtherPage();
    }, 1000);
  }
}
initOtherPage();

Console output

Opened page


Need Your Help

How to Filter the Html Table list in asp.net mvc by using jquery in client side

javascript jquery asp.net-mvc

Hi i'm trying to filter the html table list in asp.net mvc by using jquery on the change of dropdown ,DropDown Contain the Product Name and i want to filter the table row which contain

Crypto.VerifyHashedPassword throws exception

c# asp.net asp.net-mvc asp.net-mvc-4 cryptography

I have hashed the user password using Crypto.HashPassword('nakedPassword'); and saved this value in my db.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.