I have a javascript like the following on my main page: It just defines a variable
test1 = function(){
var bla= "bla";
}
In my iFrame, I have another javascript which only produces an alert like the following
alert(parent.test1.bl);
Unfortunately, the alert window appears but the content says “undefined”. How can I read a variable from within a function in the parent window?
The funny part is that if I execute the alert at the parent window and call only the function from within the iFrame, the alert box pops up and the message is included. But it seems that it is not possible to read a variable from the parent.
Functions haven’t properties, objects have. So you’ve to instantiate
test1()before trying to use its properties.Notice the usage of the keyword
thishere, which refers to the newly created object, and makesbla property, that can be read outside of the function.bladeclared withvarcan’t be seen outside oftest1(), unless you won’t create another property likethis.bl = bla;The “funny part” of your question really is unclear, definitely your
alert()will showundefined, no matter where it’s invoked.Functions and Objects at MDN.