在不同的.js文件之间传递变量 [英] Pass variables between different .js files
问题描述
< script type =text / javascript SRC = JS / 1.js >< /脚本>
< script type =text / javascriptsrc =js / 2.js>< / script>
该文件也有一个iframe。我在iframe中有2个.js:
< script type =text / javascriptsrc =js / 2的.js>< /脚本>
< script type =text / javascriptsrc =js / 3.js>< / script>
所以2.js在两个文件中。我的计划是将它连接起来。
我不能在这两个文件中放入3.js,因为它会弄乱东西。
1.js有一个变量。我想在3.js中使用该变量。但我不知道如何将变量从1.js传递到3.js.
这甚至有可能吗?
*变量在1.js中声明。
你不能通过文件引用传递变量。您需要添加代码以将数据从父框架传递到iframe。
如果变量是全局变量,则它是
// // iframe
var theVariable = window.parent.yourVaraibleName;
//从父元素
var theVariable = document.getElementById(iframeId)。contentWindow.yourVaraibleName;
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>
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>
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 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?
*The variable is declared in 1.js.
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文件之间传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!