0

我有我的网站

www.aplicatii-iphone.ro

和另一个

本地主机上的 page.html

<html>
<head>
<title>Object References across Iframes</title>

<script type="text/javascript">

window.onload = function(){
    var form = document.getElementById('testForm');
    form.testBtn.onclick = sendData;
}

function notify() {
    //alert('iframe loaded');
    var iframeEl = document.getElementById('ifrm');
    if ( iframeEl && iframeEl.parentNode && document.createElement ) {
        var newTxt = document.createTextNode('The iframe has loaded and your browser supports it\'s onload attribute.');
        var newPara = document.createElement("p");
        newPara.className = 'demo';
        newPara.appendChild(newTxt);
        iframeEl.parentNode.insertBefore(newPara, iframeEl);
    }
}

function sendData() { // to form inside iframed document
    // frames array method:
   // window.frames['ifrm'].document.forms['ifrmTest'].elements['display'].value = this.form.testEntry.value;
    var ifrm = document.getElementById('ifrm');
    var doc = ifrm.contentDocument? ifrm.contentDocument: ifrm.contentWindow.document;
    var form = doc.getElementById('search-input'); // <------<< search input
    form.display.value = this.form.testEntry.value;
    form.submit();
}

// test in iframed doc
var counter = 0;

</script>
</head>
<body>

<form id="testForm" action="#">
<p><input type="text" name="testEntry" size="30" value="[enter something]" /> <input name="testBtn" type="button" value="Click Me" /></p>
</form>

<iframe name="ifrm" id="ifrm" src="http://www.aplicatii-iphone.ro" onload="notify()" width="900">Sorry, your browser doesn't support iframes.</iframe>

</body>
</html>

每次我按下按钮Click Me时,我希望www.aplicatii-iphone.ro的状态就像用户从 iframe 外部搜索写在“testEntry”中的值一样。

我在那里尝试了一些东西......但我无法弄清楚......有什么帮助吗?

我从这里http://www.dyn-web.com/tutorials/iframes/refs.php

4

2 回答 2

3

If you know you're using a modern browser, you could use postMessage to communicate between the frames. Here's a good write-up: http://ajaxian.com/archives/cross-window-messaging-with-html-5-postmessage

If you need to support legacy browsers, you could use Google Closure's CrossPageChannel object to communicate between frames.

于 2012-05-29T16:57:28.577 回答
3

不幸的是,由于相同的原产地政策,这是不可能的。只有当您尝试将子域与主域连接时
,更改-value 才会有所帮助。document.domain

编辑 如果您通过使用同一网站上的页面来避免相同的组织问题,这应该适合您:

window.frames['ifrm'].document.getElementById("search-input").value = document.getElementsByName("testEntry")[0].value;
window.frames['ifrm'].document.getElementById("cse-search-box").submit();
于 2012-05-29T16:51:51.777 回答