Using script tags within script break the script

一个人想着一个人 提交于 2019-12-13 03:57:38

问题


Essentially, I have a script tag within my script.

(generic HTML)
<script>
function asdf(){
    document.getElementById('jkl').innerHTML = "<script>(another script goes here)</script>"
}
</script>
(generic HTML) 

Unfortunately, the first </script> tag is listened to, not the second. Is there any way to "comment" it, like butting a back slash in front of quotes?


回答1:


You need to break your inside script string into two pieces like this:

<script>
function asdf(){
    document.getElementById('jkl').innerHTML = "<script>(another script goes here)</scr" + "ipt>"
}
</script>

Otherwise the HTML parser will think that the inner </script> closing tag is closing the opening tag, and this will cause problems.



来源:https://stackoverflow.com/questions/20162699/using-script-tags-within-script-break-the-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!