最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Javascript changing values inside a iframe - Stack Overflow

programmeradmin5浏览0评论

I have my website

www.aplicatii-iphone.ro

and another

page.html on localhost

<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="; onload="notify()" width="900">Sorry, your browser doesn't support iframes.</iframe>

</body>
</html>

And every time I press the button Click Me, I want that the state of www.aplicatii-iphone.ro to be like a user searched for that value written in "testEntry" from outside of the iframe.

I tried something there ... but I can't figure it out ... any help please?

I took the example from here .php

I have my website

www.aplicatii-iphone.ro

and another

page.html on localhost

<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>

And every time I press the button Click Me, I want that the state of www.aplicatii-iphone.ro to be like a user searched for that value written in "testEntry" from outside of the iframe.

I tried something there ... but I can't figure it out ... any help please?

I took the example from here http://www.dyn-web./tutorials/iframes/refs.php

Share Improve this question edited Apr 1, 2021 at 8:16 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 29, 2012 at 16:21 Master345Master345 2,28611 gold badges39 silver badges51 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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

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

Unfortunatly, this is not possible due to the Same orgin policy.
And changing the document.domain-value only helps if you try to connect a subdomain with the main-domain.

Edit If you avoid the same-orgin-problem by using a page on the same website, this should work for you:

window.frames['ifrm'].document.getElementById("search-input").value = document.getElementsByName("testEntry")[0].value;
window.frames['ifrm'].document.getElementById("cse-search-box").submit();