Post to IFRAME

Sometimes there is the need to embed some site inside another and an IFrame because a necessary evil.

But then there is the need to comunicate the two sites.

A common need is to send credential from one site to the other.

How can that be achieved??

Well after searching the web. I found that there are many alternatives.

  1. Sending messages.

Frames are mostly like separate windows. However you can still comunicate between them.

The api is simple. The page that hosts the iframe needs a reference to iframe element and can then just post any string or json.

    //select the iframe containing the message receiver remote script
    var remoteframe = document.getElementById("remotepage");
    var message = "some message"
    remoteframe.contentWindow.postMessage(message, '');

The receiving frame needs to susbcribe a message listener and should validate the message origin for safety:

  //This function is called whenever the window receives a message event, the message object is passed in as it's only parameter

function receiver(message) {
    var trusteddomain = "";

    //use the origin property of the message event to 
    //ensure the message originates from the same domain)
      if (message.origin == trusteddomain) {
        //Do your accions.

//Add an event listener to the window object to catch any message events
window.addEventListener('message', receiver, false);  

You can see a working example at:
The source for the receiving frame is at:

  1. Post to IFRAME

Another simple option is to post directly to the iframe:

You can do something like:

<form ... target="my_iframe">

<iframe name="my_iframe" ...></iframe>

And when you post the form the iframe will receive the result.

You can do it completely in typescript as described here:

function sendData(data,iframeName,url) {
  var name;
  var form = document.createElement("form"),
  var node = document.createElement("input");

  form.action = url; =;

  for(key in data) {  = key;
    node.value = data[key].toString();

  // To be sent, the form needs to be attached to the //main document. = "none";


  // Once the form is sent, remove it.

And use it with something like