Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

javascript - Pass variables between different .js files; one is inside an iframe

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...