10

A MainWindow creates a JavaScript object that the ChildWindow needs to utilize utilize.

My MainWindow.html looks like this at the moment

<html>
  <body>
    <script>
      var varObject = {type:"Error", message:"Lots"};
    </script>
    <iframe class="child" src="ChildWindow.html"></iframe>
  </body>
</html>

The ChildWindow.html looks like this

<html>
  <body>
    <script>
      console.log(varObject.type); // goal is to log "Error"
    </script>
  </body>
</html>

The ChildWindow is trying to use the object that was created in the MainWindow which of course it can't because I don't yet know how to pass it.

I've tried to Google this but most of the solutions I found involved passing the values as strings instead of as a variable.

3 Answers 3

13

One can simply pass the object by assigning the object to the window of the iframe.

in the parent window:

var frame = document.querySelector("iframe");
frame.contentWindow.object_of_interest = object_of_interest;

in the iframe'ed window

console.log(window.object_of_interest);
Sign up to request clarification or add additional context in comments.

5 Comments

Must be contentWindow instead contextWindow?
Worked for me with contentWindow, as @abdolence suggested.
SecurityError: Permission denied to access property "object_of_interest" on cross-origin object
@Oram - the "website" in the iframe must allow cross-origin access (or have the same origin) in order for you to modify its data. There are lots of security risks that could be exploited if browsers let users open other websites and modify the data in those websites with no restrictions.
I added the comment because you can't "simply pass the object". You have restrictions as you mentioned in your reply.
6

Please have a look at following code :

<html>
  <body>
    <script>
      var varObject = {type:"Error", message:"Lots"};
      var child = document.getElementsByClassName("child")[0];
      var childWindow = child.contentWindow;
      childWindow.postMessage(JSON.stringify(varObject),*);
    </script>
    <iframe class="child" src="ChildWindow.html"></iframe>
  </body>
</html>

In ChildWindow.html

<html>
  <body>
    <script>
      function getData(e){
        let data = JSON.parse(e.data);
        console.log(data);
      }
      if(window.addEventListener){
        window.addEventListener("message", getData, false);
      } else {
        window.attachEvent("onmessage", getData);
      }
    </script>
  </body>
</html>

Hope it helps :)

Comments

3

You should use window.postMessage to send messages to and from iFrames embedded in your site.

3 Comments

It's not necessarily going to be a message, sometimes it will be database connection objects
You can use JSON.stringify(obj) to convert your objects to strings and JSON.parse(str) to convert them back again.
This does not work if the object is a promise or a function or any other more complex object structure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.