I guess what I would do is completely change how you are doing this.
Instead of creating the contents of the popup from the main window, I'd create a *SEPARATE* HTML page for the popup and I would *PASS* the arguments from the main window to it, in the query string as part of its URL. Then in the popup I parse the arguments and create the contents. This means that all the needed JS code can already be there in the HTML for the popup.
Though it means a rewrite, it's actually a much simpler rewrite.
Up to you.
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.