admin管理员组

文章数量:1026989

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.

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.

本文标签: javascriptwindowdocumentwritewrite html element and moreStack Overflow