Get Variable from PARENT Webpage (C2 or C3, Game Embedded in iFrame)

3

Tagged

Stats

753 visits, 961 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 2 Feb, 2021.

Within the PARENT Web Page, Declare your variable(s)

Example:

var my_var = 'hello world!';

Embed your C2 in C3 game in the PARENT web page inside an iFrame:

	<html>
		<body>
    <iframe id="c3" src="http://www.yourwebsite/yourfolder/index.html" style="border:none;">
    </iframe>
		</body>
	</html>

Within your C2 or C3 project:

Create a GLOBAL VARIABLE:

On Event, Button Click, etc..

System SET VARIABLE VALUE

See Image Below:

  • 3 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • I'm away from my computer, so I can't test this yet, but it looks pretty good and useful. Can something like this be used to change the variable in the parent page from the iframe project? Instead of using postMessage? (I wish I was at my computer right now...)

    • Yes!! I tested your tutorial, and it works better than I expected: I can set the parent page var with something like Browser.ExecJS("parent.my_var = 5;") or ("parent.my_var = parent.my_var + 1;")

      I realized that this parent variable can then be passed to another parent page's variable via localstorage and then read from that variable by a separate Construct project embedded in an iframe there. I also learned that the "parent" can refer to the index.html that the Construct project is embedded in (if that page is not embedded in an iframe) or to the parent page containing the iframe containing the Construct project. In that case, "parent.my_var" ignores the index.html and refers to the main page containing the iframe. I haven't fully tested that whole process yet, but it seems doable. Thanks for the tutorial!

  • Thanks for sharing but went above my head as I feel the information is not complete. where the declared global variable is used?