views:

323

answers:

2

I'm trying to create hidden iframes in my page dynamically to load 3 other pages so that i can grab all the HTML and combine them into 1 in a new window.

However i'm stuck at this. tHe frames are created fine. But whenever the javascript runs to the part of

 var printWindow="";
 function openNewWindow()
 {
     printWindow = window.open("","");
     printWindow.document.open();
     printWindow.document.write(HTMLfromFrames);
     printWindow.document.close(); 
 }

i get this error: TypeError: Result of expression 'printWindow' [undefined] is not an object.

but if i generate a button to call this function seperately, it works fine. however i need it to run all in one click event

Anybody has any idea what's wrong? Thanks!

A: 

It looks to me like a scoping problem. The scope of your printWindow object ends when openNewWindow returns; in other words, the variable only exists inside that function and disappears as soon as the function ends. Remove the var to make the variable available globally (considered bad form) or declare the variable elsewhere in your code and make sure it's available to openNewWindow when it executes.

Andrew
openNewWindow() will only run last of all the functions that executes via the Event onClick.Anyways,I've declared it as a global variable but it still seem to encounter the same issue :SI wonder why.
orangebrainer
If you are still re-declaring the variable inside that function, it will be made local to that function and will not be aware of printWindow in the outer functions or in the global namespace. Try removing the `var`.
Andrew
yes i did. I'm still having the same problem.
orangebrainer
Can you post the code in the button that gets the whole process started? Also, can you make sure that it is not itself inside a different frame from the code above?
Andrew
A: 

oh i solved it. SOmehow i declared as a global var then declare the obj earlier in the method. printWindow = window.open("",""); still not sure why i can't declare it after i dynamically create my iframes. Thanks for the help!:D

orangebrainer