我可以在<script>标记内写一个<script>标记吗?

Ama*_*dra 3 html javascript

这是我的代码.当屏幕宽度小于1025时,我试图停止运行外部js文件.

<script type="text/javascript">
var gdsize = 1025;          
if(window.innerWidth>=gdsize) {

    <script>
    window.cookieconsent_options = {"message":"\"Geek Dashboard uses cookies to make sure you get the best experience on our website.\" -","dismiss":"It's OK","learnMore":"More Details Here","link":"http://www.geekdashboard.com/cookies/","theme":"dark-bottom"};
    }
    </script>


</script>

<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/1.0.9/cookieconsent.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

Vik*_*ant 6

<script type="text/x-jQuery-tmpl">
    var gdsize = 1025;          
    if(window.innerWidth>=gdsize) {

    <script type="text/javascript">
        <!-- Some javascript here -->
        window.cookieconsent_options = {"message":"\"Geek Dashboard uses cookies to make sure you get the best experience on our website.\" -","dismiss":"It's OK","learnMore":"More Details Here","link":"http://www.geekdashboard.com/cookies/","theme":"dark-bottom"};
        }
    {{html "</sc"+"ript>"}}
</script>
Run Code Online (Sandbox Code Playgroud)

你可以{{html "</sc"+"ript>"}}代替使用</script>

说明[更新]:

</script>在带引号(文字)字符串中使用HTML标记时,标记将被视为结束标记而不是字符串的一部分.因此,您无法</script>在脚本部分中直接使用该标记.

一种解决方法是转义</script>标签和/或拆分<script>标签:

var scriptEnd = "<\scr" + "ipt>";
document.write(scriptEnd);
Run Code Online (Sandbox Code Playgroud)


Sho*_*omz 2

为什么要使用嵌套脚本来修改对象属性?

由于您已经“在脚本中”,因此可以用更简单的方式解决它:

<script type="text/javascript">
  var gdsize = 1025;          
  if(window.innerWidth>=gdsize) {

    window.cookieconsent_options = {"message":"\"Geek Dashboard uses cookies to make sure you get the best experience on our website.\" -","dismiss":"It's OK","learnMore":"More Details Here","link":"http://www.geekdashboard.com/cookies/","theme":"dark-bottom"};

  }    
</script>
Run Code Online (Sandbox Code Playgroud)