Skip to content

Instantly share code, notes, and snippets.

@jmg
Last active November 4, 2019 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmg/da839535e45c69ba81a26b9eb05f1c4f to your computer and use it in GitHub Desktop.
Save jmg/da839535e45c69ba81a26b9eb05f1c4f to your computer and use it in GitHub Desktop.
Recibe un archivo desde una pestaña (parent.html) mediante FileReader y postMessage
<!DOCTYPE html>
<html>
<head>
<title>Child Window</title>
<script type="text/javascript">
//esta funcion escucha eventos desde parent.html para recibir los datos del archivo
window.addEventListener('message', function(e) {
//convierto los datos en formato JSON a su "formato original" (objeto de javascript)
var message = JSON.parse(e.data);
if (message.type == "file") {
//muesto el archivo en el html de child.html
document.getElementById('file-content').innerText = message.data;
}
});
//envio la señal de "ready" a parent.html
window.opener.postMessage("ready", '*');
</script>
</head>
<body>
<div id="file-content"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment