4

How can I use JavaScript/jQuery to populate a popup-window with data from JS-variables in the parent page?

In my example I have an array of filenames. I list at most five in the parent window and if there's more I want to provide a link that opens a popup window and lists each post in the array.

So if I open a popup that contains a <ul id="all_files"></ul>, how can I add <li>-items to that list?

Edd Morgan
  • 2,873
  • 17
  • 22
Christoffer
  • 25,035
  • 18
  • 53
  • 77

1 Answers1

4

Parent Window:

<span id="popup"> Click to Open Popup </span>

<script type="text/javascript">
var ar=new Array("Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", 
                 "Item 7", "Item 8", "Item 9", "Item 10");
function getArray(){
    return ar;
}
$(document).ready(function(){
    $("span#popup").click(function(){
        var p=window.open("Popup.html");
    });
});
</script>

Popup Window:

<ul id="list"></ul>
<script type="text/javascript">
    if(window.opener && !window.opener.closed){
        var ar= window.opener.getArray();
        var items="";
        for(var i=0;i<ar.length;i++){
            items +="<li>" + ar[i] + "</li>";
        }
        $("ul#list").html(items);
    }
</script>
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
  • It stops executing at ar ar= window.opener.getArray(); and the Firefox console outputs: "Error: window.opener.getArray is not a function". Even though it exists and is not misspelled. – Christoffer Nov 16 '09 at 11:28
  • incidently I've tested it in Firefox 3.5.x with firebug! – TheVillageIdiot Nov 16 '09 at 12:03
  • Sorry, it was my bad... Actually this function is called from an Iframe and I believed that that frame would act as the parent... but that was wrong. So to access the Iframe's JS-function one can do this: window.opener.window.frames.THIS_FRAME.getFiles(); where the name AND id of the Iframe is "THIS_FRAME". – Christoffer Nov 16 '09 at 12:35