最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Oct 15, 2019 at 14:51 ToolmakerSteve 21.2k16 gold badges108 silver badges221 bronze badges asked Jun 18, 2013 at 15:17 KoiskiKoiski 5682 gold badges8 silver badges23 bronze badges 3
  • What do you mean by "pass". You mean global scope, from-to a function. Just by including 1 file after the other makes all global scope variables visible to the next file. – Precastic Commented Jun 18, 2013 at 15:19
  • 2 From the code in the <iframe>, a variable in the parent window can be accessed as window.parent.someVariable – Pointy Commented Jun 18, 2013 at 15:19
  • 1 2.js is not the same element in the 2 pages, what you are doing is instanciate one in the main HTML and on other in the iframe, so you won't connect them in this way – Jaay Commented Jun 18, 2013 at 15:20
Add a comment  | 

4 Answers 4

Reset to default 11

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;

Why not using jQuery cookies to pass the variables? Even within the multiple pages. Once you pass the variable you can destroy the cookie.

You cannot pass variables from one js file to the other Javascript variables are stateless so they wont be retained. If you are using .Net then you can make use of Session variables to solve this purpose. If you use MVC you can go for viewbag or viewdata. If its a must then declare some variable in the homepage, then assign the value to be passed to the variable in home page and then call the function in 3.js passing this parameter.

jus create a global variable, don't use var keyword

myGlobal = "probably not a good idea but still";
发布评论

评论列表(0)

  1. 暂无评论