Cómo cambiar el color de fondo de un iframe a otro color? (En caso de un crossorigin error, por ejemplo, tratar de cambiar el css de Wikipedia, mediante un iframe, no podrán acceder a iFrame.contentWindow.document):
<!DOCTYPE html>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<iframe id="iframe_example" src="http://localhost/pagina.html" title="Página prueba"></iframe>
<script>
$( document ).ready(function() {
$("#iframe_example").on("load", function() {
getFrameContents();
});
function getFrameContents(){
var iFrame = document.querySelector("#iframe_example");
alert("Cambiando el fondo del iframe a azul.");
iFrame.contentWindow.document.querySelector("body").style.backgroundColor = "blue";
}
});
</script>
</body>
No hay comentarios:
Publicar un comentario