Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

javascript - Can I write a <script> tag inside a <script> tag

This is my code. I'm trying to stop running an external js file when screen width is less than 1025.

<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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
<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>

You can use {{html "</sc"+"ript>"}} in place of </script>

Explanation [update]:

When you use a </script> HTML tag inside a quoted (literal) string, the tag is treated as a closing tag rather than as a portion of the string. So you cannot directly use the </script> tag inside a script section.

One work-around is to escape the </script> tags and/or split up the <script> tags:

var scriptEnd = "</scr" + "ipt>";
document.write(scriptEnd);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...