在该页面内使用iframe共享页面的全局javascript变量 [英] Sharing global javascript variable of a page with an iframe within that page
本文介绍了在该页面内使用iframe共享页面的全局javascript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个场景,我有一个页面,其中包含< script>
标签和一些全局javascript变量。我在该页面中也有一个iframe,我想在iframe中访问该页面的全局javascript变量。
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?
推荐答案
通过在iframe的脚本中调用parent.your_var_name轻松实现。
Easily by calling parent.your_var_name in your iframe's script.
一个条件:两个页面(主页面和iframe)必须位于同一个域中。
One condition: both pages (main and iframe's) have to be on the same domain.
主页:
<script>
var my_var = 'hello world!';
</script>
iframe
<script>
function some_fn(){
alert(parent.my_var); //helo world!
}
</script>
这篇关于在该页面内使用iframe共享页面的全局javascript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文