Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!