• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

209
Vistas
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.

over 3 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda