me general way, the ideal that the communication between different documents is made using message exchange.
Let’s take the following use case, the page should send a text (value
of textarea
) to the iFrame
, the iFrame
should update its content based on the received text, then should notify the parent page for it to clear the textarea
.
to achieve such a result, you can make use of the window.postMessage(...)
and of window.addEventListener('message', function (evt) { ... })
// html/script do iFrame
var html = `
<html>
<head><\/head>
<body>
<div id="box">
<\/div>
<script type='text/javascript'>
var box = document.getElementById('box');
window.addEventListener('message', function (evt) {
if (evt.origin !== 'null') // modifique o 'null' para a dominio do iframe.
return;
box.textContent = evt.data
window.parent.postMessage('OK', '*') // modifique o '*' para a dominio da pagina pai.
})
<\/script>
<\/body>
<\/html>
`;
// criando a pagina a ser carregada no iFrame.
var blob = new Blob([html], { type: 'text/html' })
document.querySelector('iframe').src = URL.createObjectURL(blob)
var iframe = document.querySelector('iframe')
var textarea = document.querySelector('textarea')
var button = document.querySelector('button')
// enviando uma mensagem para o iframe
button.addEventListener('click', function (evt) {
iframe.contentWindow.postMessage(textarea.value, '*') // modifique o '*' para a dominio do iframe.
})
// recebendo uma mensagem do iframe
window.addEventListener('message', function (evt) {
if (evt.origin !== 'null' || evt.data !== 'OK') // modifique o 'null' para a dominio da pagina.
return;
textarea.value = '';
})
textarea, iframe {
width: 300px;
height: 120px;
}
<div>
<textarea></textarea>
<iframe src='#'></iframe>
</div>
<div><button>Copiar</button></div>
Yes it is. It’s just below a Dropdown
– Carmona007
On the same page. Dropdowm above and Iframe below. I want to take the Dropdown value and send it to the page in the IFRAME SRC
– Carmona007
Yes it is of the Same.
– Carmona007
Yes I’m using ASP Classico.
– Carmona007
From the parent page. The Code I sent is just to get the value of the combo. Now I need to play for the iframe.
– Carmona007