我可以写一个<脚本>在<脚本>内部标记标签 [英] Can I write a <script> tag inside a <script> tag
问题描述
这是我的代码。我试图在屏幕宽度小于1025时停止运行外部js文件。
< script type =text / JavaScript的>
var gdsize = 1025;
if(window.innerWidth> = gdsize){
< script>
window.cookieconsent_options = {message:\极客仪表板使用Cookie来确保您在我们的网站上获得最佳体验。 - ,解雇:没关系,learnMore :更多细节在这里,链接:http://www.geekdashboard.com/cookies/,主题:深色底部};
}
< / script>
< / script>
< script type =text / x-jQuery-tmpl>
var gdsize = 1025;
if(window.innerWidth> = gdsize){
< script type =text / javascript>
<! - 一些JavaScript在这里 - >
window.cookieconsent_options = {message:\极客仪表板使用Cookie来确保您在我们的网站上获得最佳体验。 - ,解雇:没关系,learnMore :更多细节在这里,链接:http://www.geekdashboard.com/cookies/,主题:深色底部};
}
{{html< / sc+ript>}}
< / script>
您可以使用 {{html< / sc+ript> ;}}
取代< / script>
解释[update]:
当您在一个内部使用< / script>
引用(字面)字符串,标签被视为结束标签而不是字符串的一部分。所以你不能直接在脚本部分使用< / script>
标签。
一个解决方法是转义< / script>
标记和/或分割< script>
标记:
var scriptEnd =< \scr +ipt>;
document.write(scriptEnd);
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>
<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);
这篇关于我可以写一个<脚本>在<脚本>内部标记标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!