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

142
Visualizações
Change global var inside a JSDom document

I'm trying create a JSDom document that contains a global var and then trying to change it's value programatically and then print the output again.

My code is the following:

    const { JSDOM } = require('jsdom');

    const dom = new JSDOM(`
        <!DOCTYPE html>
        <html lang="en">
        <meta charset="UTF-8">
        <body>
            <script>
                var myGlobalVar = 'foo';
            </script>
        </body>
        </html>`,
        { runScripts: 'dangerously' });

    dom.window.myGlobalVar = 'bar';

    console.log(dom.serialize())

I would expect the output to be the same document but with the value of myGlobalVar updated to "bar". This is not happening and myGlobalVar is still "foo".

Does anyone know what I'm doing wrong and if what I want is possible?

Thanks in advance for any help.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

dom.window.myGlobarVar = “bar” sets a property on the window object it doesn’t change the content of the DOM you have provided to the JSDom constructor. If you console.log(dom.window.myGlobalVar), you should see the output as bar.

about 4 years ago · Juan Pablo Isaza 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