在不同的.js文件之间传递变量;一个在iframe内 [英] Pass variables between different .js files; one is inside an iframe

查看:210
本文介绍了在不同的.js文件之间传递变量;一个在iframe内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在html文档中有2个.js文件,如下所示:

I have 2 .js files in the html document like this:

    <script type="text/javascript" src="js/1.js"></script>
    <script type="text/javascript" src="js/2.js"></script>

该文档也有一个iframe.我在iframe中也有2个.js:

That document also have an iframe. I have 2 .js in the iframe aswell:

    <script type="text/javascript" src="js/2.js"></script>
    <script type="text/javascript" src="js/3.js"></script>

两个文档中都包含2.js.我的计划是使它们连接起来. 我不能在两个文档中都放3.js,因为它会弄乱东西.

So 2.js is in both documents. My plan was to make that to connect them. I can not put 3.js in both documents because it will mess up stuff.

1.js有一个变量.我想在3.js中使用该变量.但是我不知道如何将变量从1.js传递到3.js. 这有可能吗?

1.js got a variable. I want to use that variable in 3.js. But i can't figure out how to pass a variable from 1.js to 3.js. Is this even possible?

*该变量在1.js中声明.

*The variable is declared in 1.js.

推荐答案

您不能通过文件引用传递"变量.您将需要添加代码以将数据从父框架传递到iframe.

You can not "pass" variables through file references. You would need to add code to pass data from the parent frame to the iframe.

如果变量是全局变量,则为

If the variable is global it is

//from the iframe
var theVariable = window.parent.yourVaraibleName;

//from the parent
var theVariable = document.getElementById("iframeId").contentWindow.yourVaraibleName;

这篇关于在不同的.js文件之间传递变量;一个在iframe内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆