Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

223
Visualizações
Manipulating and adding <script> to iframe

Let's say I have access to an index.html and within this page, I'm loading a contact.html as iframe.

I can manipulate this iframe and here's my code:

const myIframe = document.getElementById("myIframe");  

const iframeDocument = myIframe.contentDocument;
     
iframeDocument.body.style.backgroundColor = "#ff0000";

I can change background color within this iframe, but what I need to do is to append JavaScript code to the <body> of this iframe document. I tried with innerHTML but it's not working.

The code that I need to append looks like this:

<script>
  iframe(function (event) {
    const frameTest = true;
    if (!iframe) {
      test(event);
    } else {
      testo(event);
    }
  </script>

It's important to add this code with tags and execute it later on.

Do you have any idea how to do it?

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

First, I created an empty page called bar.html

<html>
        <head>
        </head>
        <body>
        </body>
</html>

then created a foo.html page as well:

<html>
        <head>
                <script>
                function load() {
                    const myIframe = document.getElementById("myIframe");
                    const iframeDocument = myIframe.contentWindow;
                    var script = document.createElement("script");
                    script.innerText = "alert(1)";
                    iframeDocument.document.body.appendChild(script);
                }
                </script>
        </head>
        <body onload="load()">
                <iframe id="myIframe" src="/bar.html">
        </body>
</html>

Please test this as a proof-of-concept

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda