1

I am trying to format this line of code in my popup window, but i am facing unterminated string literal error.

Can somebody please tell me how best I could format this.

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

Also point out if this particular line of code would work fine in the popup?

6 Answers 6

4

Best not to use a string, but an anonymous function instead:

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 might also be worth noting that document.write() will not work correctly on an already parsed document. Different browsers will give different results, most will clear the contents. The alternative is to add the 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);
Sign up to request clarification or add additional context in comments.

10 Comments

@Andy thanks a lot for the help! I have a quick question. The solution is working i.e the JS file is getting included in the popup only on IE6 and Opera and is failing on Chrome,IE8 and Safari.Any idea on why this might be happening?
@dkris: any error messages? What is the location of your popup window and, does the relative JS source match up with it? Also, in Chrome, use the developer tools (Ctrl+Shift+I) to inspect the elements in the document to see if the script element is successfully added to the popup.
@Andy just noticed on Chrome Uncaught ReferenceError: tinyMCE is not defined . I guess this means its not loading the source file
@Andy The TinyMCE script is not loading and desired effect is not achieved. Here is some additional info - Screen shot of the page img231.imageshack.us/img231/7190/72961203.png - Rendered HTML pastebin.org/295861 - Original JSPF pastebin.org/297641
@Andy here is what I have finally used. But i guess i'm missing something important here pastebin.org/297670 and I have removed the setTimeout
|
3

You may try it by escaping the 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);

5 Comments

I am still having the same unterminated string error in the firebug console after using the above snippet
Check out stackoverflow.com/questions/1081573/… and try \x22 instead of \"
Other than that, I second the anonymous function mentioned in an other answer, it's a nicer solution.
+1 for your suggestion as you pointed out the \x22 method. Thanks for helping out.
Now it should be working - escaping < and > with a backslash was also needed. The same for \n which became \\n.
2

Aside from you needing to escape your quotes (as other people have mentioned), you cannot include "</script>" (even if it's within a string) anywhere within a <script> tag

Use:

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

Instead.

3 Comments

+1, it didn't occur to me that the code might have been residing in a script tag, I'm surprised I missed that. I prefer to use <\/script> rather than concatenating, though.
@Matt So does this mean I need to have a matching closing tag for the script tag in @Andy's approach?
@dkris: If you're using Andy's first approach, you'll need to escape the "</script>" using either "</scr' + 'ipt>" or "<\/script>". If you're using his second idea, you don't need to alter anything.
1

It's because you're using nested double quotes. Quotes delimit strings so when you get to the second one, it thinks the string has ended, as you can see from the colour highlighting in the code you posted. You need to escape them with \":

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

1 Comment

I am still having the same unterminated string error in the firebug console after using the above snippet
0

You have to escape the " in your script tag attributes with \" so it would read:

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.