2014-02-13 51 views
0

在我的html頁面中(file:/// C:/Users/NEC/Desktop/test.html)有調用其他主機的javascript變量(或輸入文本字段)和iframe(localhost:8081/TProject/sample的.html)。如何將數據從localhost的iframe設置爲離線html文件的父窗口變量?

在test.html的

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

而且,在iframe顯示我sample.html有跟隨javascript代碼改變父窗口的輸入文本的價值。

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

錯誤消息:

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. 

如何從在線服務器值(JavaScript變量)傳遞給離線的html頁面?

+0

使用的postMessage()不同協議幀之間的交談。 – dandavis

+0

類似的文章在http://stackoverflow.com/questions/5604839/accessing-an-element-outside-of-iframe – 2014-02-13 05:21:56

+0

我的父頁面處於脫機狀態。所以我不能發送消息給使用postMessage()isFailed在'DOMWindow'上執行'postMessage'的父錯誤消息:提供的目標原點('http:// localhost:8081')與收件人窗口的原點不匹配'空值')。 –

回答

0
<iframe src="http:80//TProject/sample.html" ></iframe> 

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

我認爲你不能在src屬性中使用localhost,請嘗試在src屬性中的上述任何一個鏈接。

2

謝謝你的回答。 現在我可以通過使用「window.parent.postMessage()」來解決我的問題。

從iframe中使用本地主機服務器頁面下面要發送消息

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

和離線網頁聽清楚消息

window.addEventListener('message', function(event) { 
    document.getElementById("output").value = event.data.message; 
}, false); 
相關問題