与该页面内的 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屋!
查看全文