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

I have opened a new window, and now I have a button in this new window.Under this button there is a J

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745665991a4639126.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信