1
<script type="text/javascript">
var randomnumber=Math.floor(Math.random()*10000);
var sr = 'http://www.example.com/example.js?rnd='+randomnumber;
document.write("<script type=\"text/javascript\" src=\"" + sr + "\"  id-value-test=\"100\" >    </script>");
</script>

getting unterminated string literal at id-value-test.Any idea?

2
  • There are better way os doing this - nevertheless, the easiest is to use nested quotes with alternatiing " and ' - document.write("<script type='text/javascript' src='" + sr + "'" id-value-test='100'> </script>"); Commented Nov 21, 2014 at 16:38
  • 1
    @karthikr: True, but nested quotes aren't the problem here. :-) Commented Nov 21, 2014 at 16:41

1 Answer 1

8

When doing the HTML parsing, browsers don't parse JavaScript, and so they stop at the first </script> sequence they find — even if it's in a string literal, or a JavaScript comment.

To avoid it, just do anything to interrupt the </script> in your string, such as:

<script type="text/javascript">
var randomnumber=Math.floor(Math.random()*10000);
var sr = 'http://www.example.com/example.js?rnd='+randomnumber;
document.write("<script type=\"text/javascript\" src=\"" + sr + "\"  id-value-test=\"100\" >    <\/script>");
<!-- Change is here -----------------------------------------------------------------------------^  -->
</script>

or

<script type="text/javascript">
var randomnumber=Math.floor(Math.random()*10000);
var sr = 'http://www.example.com/example.js?rnd='+randomnumber;
document.write("<script type=\"text/javascript\" src=\"" + sr + "\"  id-value-test=\"100\" >    </" + "script>");
<!-- Change is here ----------------------------------------------------------------------------^  -->
</script>
Sign up to request clarification or add additional context in comments.

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.