在脚本中使用脚本标记会破坏脚本 [英] Using script tags within script break the script

查看:106
本文介绍了在脚本中使用脚本标记会破坏脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我的脚本中有一个脚本标记。

 (通用HTML)
< script>
函数asdf(){
document.getElementById('jkl')。innerHTML =< script>(另一个脚本在这里)< / script>
}
< / script>
(通用HTML)

不幸的是,第一个< /&脚本> 标记被监听,而不是第二个。有没有什么办法可以评论它,比如在引号前加一个反斜杠?

解决方案

您需要将内部脚本字符串分解为两部分:

 < script> 
function asdf(){
document.getElementById('jkl')。innerHTML =< script>(另一个脚本在这里)< / scr+ipt>
}
< / script>

否则HTML解析器会认为内部的< / script> 结束标记正在关闭开始标记,这会导致问题。


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?

解决方案

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.

这篇关于在脚本中使用脚本标记会破坏脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆