javascript属性值问题

Rp9*_*Rp9 1 javascript jquery

<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>
Run Code Online (Sandbox Code Playgroud)

id-value-test获得未终止的字符串文字.任何想法?

T.J*_*der 8

在进行HTML解析时,浏览器不解析JavaScript,因此它们会在</script>它们找到的第一个序列处停止- 即使它是在字符串文字或JavaScript注释中.

为了避免它,只需要做任何事情来中断</script>你的字符串,例如:

<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>
Run Code Online (Sandbox Code Playgroud)

要么

<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>
Run Code Online (Sandbox Code Playgroud)