How do I send a variable using window.postMessage()?

1 favourites
  • 6 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Hi everyone, how to send a variable from C3 (game in iframe) to the parent webpage using window.postMessage()? What to do in C3 for that?

  • If you are on the same domain, I would go with:

    Parent window with iframe

    <input type="text" id="meh" value="nada">

    page in iframe

    window.parent.document.getElementById('meh').value= "zoinks";

    (changes nada to zoinks)

    If its cross domain

    developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

    example at the bottom

  • If you are on the same domain, I would go with:

    Parent window with iframe

    <input type="text" id="meh" value="nada">

    page in iframe

    window.parent.document.getElementById('meh').value= "zoinks";

    (changes nada to zoinks)

    Hi, sorry I don't know JS really. I only have a regular web page (not an iFrame) and this page shows the game inside the iFrame. I need to somehow send a variable (if the player won or lost) to the parent page from the game. How will it look in C3 events?

  • There currently is no built in way to do what you need.

    You would need to write some Javascript in order to use the postMessage API

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • > If you are on the same domain, I would go with:

    >

    > Parent window with iframe

    > <input type="text" id="meh" value="nada">

    >

    >

    > page in iframe

    > window.parent.document.getElementById('meh').value= "zoinks";

    >

    > (changes nada to zoinks)

    Hi, sorry I don't know JS really. I only have a regular web page (not an iFrame) and this page shows the game inside the iFrame. I need to somehow send a variable (if the player won or lost) to the parent page from the game. How will it look in C3 events?

    This example requires some JS through the add script method, rather old fashioned dom approach. Gave the construct input an ID, gets fetched on button press and send to a parent, will give an error if not there.

    You can change the input in the app, then press a button

    boomtanium.com/construct/test/iframe/pagewithframe.html

    Here are all the files packaged:

    - html page with iframe (pagewithframe.html Make sure to update the 2 URLs in the page to match your server.

    -php response page

    -c3p file, and exported

    dropbox.com/s/e0pw2pmpolwui9t/iframe.zip

    unzip, updateurls in html page, upload the entire folder, or its contents to a desired server and open yoursite.com/pagewithframe.html in a browser.

  • Thank you! Will check it now.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)