Tuesday, June 4, 2024
 Popular · Latest · Hot · Upcoming
16
rated 0 times [  17] [ 1]  / answers: 1 / hits: 74469  / 13 Years ago, tue, october 4, 2011, 12:00:00

I have an scenario where in I have a page, which have <script> tag and some global javascript variables within that. I also have an iframe within that page and I want to have access of global javascript variables of the page in iframe.



Is it possible?, if yes, how can i do that?


More From » iframe

 Answers
82

Easily by calling parent.your_var_name in your iframe's script.



One condition: both pages (main and iframe's) have to be on the same domain.



main page:



<script>
var my_var = 'hello world!';
</script>


iframe



<script>
function some_fn(){
alert(parent.my_var); //helo world!
}
</script>

[#89806] Sunday, October 2, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alonso

Total Points: 747
Total Questions: 108
Total Answers: 105

Location: Mauritania
Member since Sun, Sep 25, 2022
2 Years ago
;