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

252
Visualizações
Copying Script with innerHTML and quotes

At the bottom of my HTML page I have declared a div with the text of a JavaScript function.

<div style="display:none" id="newwinner">
function newwin()
{ nwin = window.open("","_blank", "scrollbars,menubar,toolbar, status,resizable,location");
  content = document.body.innerHTML;
  if(nwin != null)
  { nwin.document.write("<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' /></head><body>"+content+"</body></html>");
    nwin.document.close();
  }
}
</div>

Now I want to insert that somewhere else. This gets the JavaScript code:

  nw = document.getElementById("newwinner");
  var g = document.createElement("script");
  g.text = nw.innerHTML;
  var tab = document.getElementById("Maintable");
  tab.insertBefore(g, tab.childNodes[0]);

I always get the same error:

Uncaught SyntaxError: missing ) after argument list

This is how it look in Chrome

The problem is the second level of quotes, like 'Content-Type'. It looks like the single quotes are converted to double quotes here. That is what I see both in the Chrome console and when I do an alert().

I tried many variations, escaping with backslashes, using escaped double quotes, using &quot;, but none of them solved the problem. What can I do to solve this?

Note that this is specific for a script node. When I use a DIV instead it works.

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