views:

86

answers:

1

I am developing a ff extension. On one menupopup, the onpopupshowing calls a javascript function. Tha JS function extracts a list of names. Now these names have to be displayed in the same popup.

How can i get this? Basically i will need to pass the data (just as we use beans in java) to the browser from the JS function. The data can change everytime the popup is called.

A: 

You need to modify the DOM rapresenting your menupopup. For example:

<menulist>
  <menupopup id="myMenuPopup">
    <menuitem id="firstItem" label="Mozilla" value="http://mozilla.org"/&gt;
    <menuitem id="secondItem" label="Slashdot" value="http://slashdot.org"/&gt;
    <menuitem id="thirdItem" label="Sourceforge" value="http://sf.net"/&gt;
  </menupopup>
</menulist>

Now if you want to add another item:

var myMenuPopup = document.getElementById("myMenuPopup");
var newItem = document.createElement("menuitem");
newItem.setAttribute("id", "anotherItem");
myMenuPopup.appendChild(newItem);

If you want to remove an item:

var itemToRemove = myMenuPopup.getElementById("anotherItem");
myMenuPopup.appendChild(itemToRemove);

Here you can find much more:

https://developer.mozilla.org/en/Dynamically_modifying_XUL-based_user_interface

Edit:

I assume thet your function returns an array of names:

var nameLists = yourFunction();

for (var i=0; i<nameList.length; i++){
  var newItem = document.createElement("menuitem");
  newItem.setAttribute("label", nameList[i]);
  newItem.setAttribute("id", "item" + i);
  myMenuPopup.appendChild(newItem);
}

I think is better if you call the function that make items before user click on menupopup, not on the event invocation, because if you have many items it may takes a little to construct the items list.

Manuel Bitto
hey kucebe, thanks for the help. it works now. so far so good.Now I need to have the click functionality too on these newly added menuitems. putting the atttribute and forming the function is easy. I can have a function to be run when they are clicked. But i need to pass namelist[i] to the function and it will act according to the namelist parameter.If i do:newItem.setAttribute("oncommand" , finalclick(namelist[m]));it runs the function everytime the popup opens runs the function without even clicking on it
encryptor
on the other hand if i donewItem.setAttribute("oncommand" , "finalclick(namelist[m])");it doesnot open even on clicking giving error : namelist is not defined.
encryptor
I think would be better if you make your event listener like this:newItem.oncommand = function(){ finalclick(namelist[m]); }
Manuel Bitto
@kucebe after making this menupopup, each menu item also needs to have a popup... as in each newItem needs to have another popup.how can i get that? [i have posted a question for the same] http://stackoverflow.com/questions/2780696/ff-extension-a-popup-with-dynamic-menuitems-with-each-menu-item-having-another
encryptor