0

In my html page(file:///C:/Users/NEC/Desktop/test.html) has the javascript variable(or input text field) and iframe that call to other host(localhost:8081/TProject/sample.html).

In test.html

<iframe src="localhost:8081/TProject/sample.html" ></iframe><br/>
<input type="text" id="output" ><br />

And, in my sample.html shown in iframe has follow javascript code to change the value of input text of parent window.

function setData() {
  window.parent.getElementById("output").value = "hello"; 
}

Error message is:

Uncaught SecurityError: Blocked a frame with origin "localhost:8081" from 
accessing a frame with origin "null".  The frame requesting access has a protocol of 
"http", the frame being accessed has a protocol of "file". Protocols must match.

How can I pass the value (javascript variable) to offline html page from online server?

Kunj
  • 1,980
  • 2
  • 22
  • 34
Aung Thuya
  • 47
  • 5
  • use postMessage() to talk between frames on different protocols. – dandavis Feb 13 '14 at 05:20
  • Similar article at http://stackoverflow.com/questions/5604839/accessing-an-element-outside-of-iframe –  Feb 13 '14 at 05:21
  • my parent page is offline. so i can't send message to parent error message of using postMessage() isFailed to execute 'postMessage' on 'DOMWindow': The target origin provided ('http://localhost:8081') does not match the recipient window's origin ('null'). – Aung Thuya Feb 13 '14 at 05:26

2 Answers2

3

Thank you for your answers. Now I can solve my problem by using "window.parent.postMessage()".

from local host server page of iframe use following to send message

window.parent.postMessage({ message: 'successfully connected'}, "*");

and from offline page catch that message with

window.addEventListener('message', function(event) {
  document.getElementById("output").value = event.data.message;
}, false);
Aung Thuya
  • 47
  • 5
0
<iframe src="http:80//TProject/sample.html" ></iframe>

<iframe src="your_path_in_your_hard_drive/TProject/sample.html" ></iframe>

I think you cannot use localhost in the src attribute, try any one of the above link in src attribute.

shubhraj
  • 391
  • 3
  • 12