Last active
May 20, 2022 18:33
-
-
Save chokonet/247af891d0213201f82273eaa33f80d2 to your computer and use it in GitHub Desktop.
Comunicación por JS entre un Iframe y la pagina padre con diferentes orígenes (dominios), todos los mensajes se reciben en una misma función se usa el tipo_mensaje para clasificar el manejo de la información según el caso
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Javascript dentro del iframe | |
| // Recibe mensajes de la plataforma de origen donde está incrustado el iframe | |
| window.addEventListener('message', receiver, false); | |
| function receiver(event) { | |
| if(event.data.tipo_mensaje == 'valores_disponibilidad'){ | |
| // función que se encargará de procesar la información según el tipo | |
| } | |
| if(event.data.tipo_mensaje == 'valores_eventos'){ | |
| // función que se encargará de procesar la información según el tipo | |
| } | |
| } | |
| // Envía un mensaje a la plataforma donde está incrustado el iframe | |
| parent.postMessage({tipo_mensaje:"click_disponibilidad"}, "*"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Javascript en la pagina donde esta incrustado el iframe | |
| // Recibe mensajes del iframe | |
| window.onmessage = function(event) { | |
| if(event.data.tipo_mensaje == 'click_disponibilidad'){ | |
| // función que se encargará de procesar la información según el tipo | |
| } | |
| }; | |
| // Envía un mensaje al iframe | |
| const miIframe = document.getElementById("iframe-calendar"); | |
| miIframe.contentWindow.postMessage({tipo_mensaje:"valores_disponibilidad"}, "*"); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Descargar ejemplo https://github.com/chokonet/ejemplo-iframes