Formatting this javascript string

I am trying to format this line of code in a popup, but I encountered an unterminated string literal error.

Can someone please tell me how best to format this.

 window.setTimeout("winId.document.write('<script src="../js/tiny_mce/tiny_mce.js" type="text/javascript"></script>\n')", 10); 

Also indicate whether this particular line of code will work fine in a popup window?

+1
source share
6 answers

It is better not to use a string, but an anonymous function:

 window.setTimeout(function () { winId.document.write( '<script src="../js/tiny_mce/tiny_mce.js" type="text/javascript"></script>\n' ); }, 10); 

Using strings in setTimeout and setInterval is closely related to eval() and should only be used in rare cases. See http://dev.opera.com/articles/view/efficient-javascript/?page=2

It can also be noted that document.write() will not work correctly with an already parsed document. Different browsers will give different results, most of them will clear the content. An alternative is to add a script using the DOM:

 window.setTimeout(function () { var winDoc = winId.document; var sEl = winDoc.createElement("script"); sEl.src = "../js/tiny_mce/tiny_mce.js"; winDoc.getElementsByTagName("head")[0].appendChild(sEL); }, 10); 
+4
source

You can try doing double quotes and <>, as well as \ n:

 window.setTimeout("winId.document.write('\<script src=\"../js/tiny_mce/tiny_mce.js\" type=\"text/javascript\"\>\</script\>\\n')", 10); 
+3
source

Besides the fact that you need to avoid your quotes (as other people have pointed out), you cannot include " </script> " (even if it is inside the line) anywhere in the <script>

Using:

 window.setTimeout(function () { winId.document.write( '<script src="../js/tiny_mce/tiny_mce.js" type="text/javascript"></scr' + 'ipt>\n' ); }, 10); 

Instead.

+2
source

This is because you are using nested double quotes. The quotes delimit the lines, so when you go to the second one, he thinks the line has ended, as you can see from the highlight in the code that you posted. You need to avoid them with \" :

 window.setTimeout("winId.document.write('<script src=\"../js/tiny_mce/tiny_mce.js\" type=\"text/javascript\"></script>\n')", 10); 
+1
source

You need to avoid "in your script tag attributes with" so that it reads:

 window.setTimeout("winId.document.write('<script src=\"../js/tiny_mce/tiny_mce.js\" type=\"text/javascript\"></script>\n')", 10); 
0
source

You can use this online tool: http://jsbeautifier.org/ best wishes

0
source

Source: https://habr.com/ru/post/1311425/


All Articles