Created
January 5, 2018 11:03
-
-
Save bruno2ms/57548e0452c27118fa36f74659916ae4 to your computer and use it in GitHub Desktop.
Acessar dados do iframe
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
// Página externa | |
// Ajusta o tamanho do Iframe com base na informação recebida pelo proprio iframe | |
// Isto é necess;ario porque os browsers não aceitam mais o acesso direto ao conteudo do iframe ou do parent | |
window.addEventListener('message', function(event) { | |
if (event.data.bodyHeight) { | |
$('#foreign').height(event.data.bodyHeight); | |
} | |
}); | |
// Página dentro do Iframe | |
// Para que este script funcione é necessário que a página que será carregada dentro do iframe contenha o seguinte script: | |
// referencia: https://stackoverflow.com/a/25098153/4009378 | |
(function resizeIframe() { | |
var iframe = window.parent; | |
var body = document.querySelector('body'); | |
parent.postMessage({ | |
bodyHeight: body.offsetHeight + 'px' | |
}, '*'); | |
setTimeout(resizeIframe, 1000); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment