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?)


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

console.log("Opening new page at");
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]


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


So doing


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 =>

The JS looks like this

page ='');

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

Console output

Opened page

Need Your Help

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

javascript jquery

Hi i'm trying to filter the html table list in 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# 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.