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":"/","theme":"dark-bottom"};
}
</script>
</script>
<script type="text/javascript" src="//cdnjs.cloudflare/ajax/libs/cookieconsent2/1.0.9/cookieconsent.min.js"></script>
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>
Share
Improve this question
asked Nov 28, 2015 at 9:03
Amar IlindraAmar Ilindra
9732 gold badges11 silver badges31 bronze badges
3
- The usual script tag (type text/javascript) is used to embed js inside markup. The tag itself is not valid js, so you cannot nest script tags (unless it's in an opaque context, such as a string). – Hunan Rostomyan Commented Nov 28, 2015 at 9:07
- <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":"geekdashboard.com/cookies/…"}; } </script> <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/1.0.9/cookieconsent.min.js"></script> – Vel Commented Nov 28, 2015 at 9:08
- jsfiddle.net/024d498L – Vel Commented Nov 28, 2015 at 9:12
4 Answers
Reset to default 14<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);
Why would you use a nested script to modify an object property?
It can be solved in a much simpler way since you're already "in the script":
<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>
I forced write a SRC atribute into SCRIPT tag, is a similar mode for write a SCRIPT tag into a SCRIPT tag. This script forced to reload a JS file every moment than load a page, prevent cache with use version ramdom number into url. Work fine, load a JS file ok:
<div id="jsgenrandom"><script type="text/javascript"></script></div>
<script type="text/javascript">
var divjs = document.getElementById('jsgenrandom')
var divjss = divjs.getElementsByTagName("SCRIPT")[0].src = "https://_route_/the_js.js?v="+ Math.floor(Math.random()*1000) + ""
</script>
Write with Javascript inside a SCRIPT tag with this solution. Work Fine:
<div id="writeintoscript"><script type="text/javascript"></script></div>
<script type="text/javascript">
var writeintoscripts = document.getElementById('writeintoscript')
writeintoscripts.getElementsByTagName("SCRIPT")[0].innerHTML = "alert('WRITE INTO SCRIPT TAG WITH JAVASCRIPT')"
</script>