最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - window.document.write - write html element and more - Stack Overflow

matteradmin4PV0评论

I have opened a new window, and now I have a button in this new window. Under this button there is a JavaScript function which should submit the form and insert some content into a <p>. This "to-add" content also has a html element (close button).

This inserting job is not working. Here is what I tried:

myWindow.document.write("<scrip" + "t>function closeme(){ document.getElementById('danke').innerHTML='thanks <input type=\'button\' onclick=\'window.close()\'/>'; } </sc" + "ript>");

Can someone please help me out?

I have opened a new window, and now I have a button in this new window. Under this button there is a JavaScript function which should submit the form and insert some content into a <p>. This "to-add" content also has a html element (close button).

This inserting job is not working. Here is what I tried:

myWindow.document.write("<scrip" + "t>function closeme(){ document.getElementById('danke').innerHTML='thanks <input type=\'button\' onclick=\'window.close()\'/>'; } </sc" + "ript>");

Can someone please help me out?

Share Improve this question edited Jan 17, 2013 at 15:39 gen_Eric 227k42 gold badges303 silver badges342 bronze badges asked Jan 17, 2013 at 14:05 doniyordoniyor 38k61 gold badges181 silver badges270 bronze badges 3
  • 2 First of all, don't use document.write - create a script element via createElement instead. This will alleviate your quotes inside of quotes inside of quotes nightmare. Second of all, nothing ever calls your closeme function? – jbabey Commented Jan 17, 2013 at 14:13
  • @jbabey, no, my function closeme is being called, i just didnot post it here, because i am sure it is being called. but generally, createElement is better way? - :))) you made me laugh! – doniyor Commented Jan 17, 2013 at 14:15
  • hunlock./blogs/Howto_Dynamically_Insert_Javascript_And_CSS – jbabey Commented Jan 17, 2013 at 14:18
Add a ment  | 

2 Answers 2

Reset to default 1

As said in the ments there are better ways to insert scripts, but in the interests of answering the question, you have to escape the escape characters as well so that they will be present in the output.

myWindow.document.write("<scrip" + "t>function closeme(){ " + 
"document.getElementById('danke').innerHTML='thanks <input type=\\\'button\\\' " + 
"onclick=\\\'window.close()\\\'/>'; } </sc" + "ript>");

(line breaks added for readability)

I agree there are better ways of doing this, but regarding your specific question. There are escaping issues:

myWindow.document.write("<scrip" + "t>function closeme(){ document.getElementById('danke').innerHTML='thanks <input type=\"button\" onclick=\"window.close()\" />'; } </sc" + "ript>");

I'd honestly add text/javascript for consistency.

Post a comment

comment list (0)

  1. No comments so far