0

Say i have two sample websites like web_1 and web_2. I am using iframe to access web_1 into web_2.

Now my question is that in web_2 username is available. I want to pass this username to iframe before it is loaded. Then i can use or access it in my application controller of web_1. Both websites are developed in ruby on rails.

  • 1
    Possible duplicate of [jQuery/JavaScript: accessing contents of an iframe](https://stackoverflow.com/questions/364952/jquery-javascript-accessing-contents-of-an-iframe) – Nayantara Jeyaraj Aug 01 '17 at 06:19

1 Answers1

2

The code in your "my application controller" will run on the web server not on browser. So, you cannot send "username" from parent(web_2) to the "application controller" of iframe(web_1).

However, you can always send "username" from parent(web_2) to client javascipt of iframe(web_1). For achieving this, use HTML5 postmessage api for communication in client javascripts .

Send messages to iframe using iframeEl.contentWindow.postMessage. Recieve messages inside iframe using window.addEventListener('message').

Example:

// In parent javascript(web_2)
window.onload = function () {
 var iframeWin = document.getElementById("da-iframe").contentWindow,
  form = document.getElementById("the-form"),
  myMessage = document.getElementById("my-message");
};

// In iframe javascript (web_1)

function displayMessage (evt) {
 var message = "I got " + evt.data + " from " + evt.origin;
 document.getElementById("received-message").innerHTML = message;
}

if (window.addEventListener) {
 // For standards-compliant web browsers
 window.addEventListener("message", displayMessage, false);
}
else {
 window.attachEvent("onmessage", displayMessage);
}
Gaurav Saraswat
  • 1,353
  • 8
  • 11