¿Será posible enviar postMessage a un iframe anidado?
Tengo una página principal [http://localhost:8765/home.html], he declarado la función postMessage en la página principal como se muestra a continuación.
// home.html [http://localhost:8765/homr.html] <html> <script> function test1(){ document.getElementById('tableauFrame').contentWindow.postMessage("success", "*") } </script> <body> child ui <br> <iframe width='2000px' height='2000px' src='http://localhost:8766/child.html' id='tableauFrame' onload=test1()></iframe> </body> </html>Dentro de la página principal, llamará a otro navegador a un iframe, en el navegador secundario tiene otro iframe que llama a la página2.
//child.html [ http://localhost:8766/child.html ] <html> <body> <p>Child page</p> <iframe src='http://localhost:8766/page2.html' id='secondary'></iframe> </body> </html>si fuera solo un iframe único [parent (home.html) to child(child.html)], funcionará y recibirá el mensaje, pero cuando probé un iframe anidado parent(home.html) to grandchild[page2.html ], no está recibiendo el postMessage.
// grandchild [ http://localhost:8766/page2.html ] <html> <script> window.addEventListener('message',function(message){ if (event.origin === "http://localhost:8765") { console.log("displaying message from parent page : " + message.origin) } }) </script> <body> <p>this is getting from 3rd page</p> </body> </html>¿PostMessage solo funciona en un solo cuadro?
Los mensajes de ventana no "burbujean"; tu también puedes:
window.parent o incluso window.top y maneje todos los mensajes publicados en la ventana superior; odocument.getElementById('tableauFrame').contentDocument.querySelector('iframe').contentWindow.postMessage("success", "*")