On my HTML page, I have the following way of loading JS files:
<script id="jjquery"></script>
<script id="jscookie"></script>
<script id="winx"></script>
<script>
(function(){
if(screen.width > 479)
document.querySelector("#jjquery").setAttribute("src", "/js/jquery-3.3.1.min.js");
document.querySelector("#jscookie").setAttribute("src", "/js/jscookie.js");
document.querySelector("#winx").setAttribute("src", "/js/winx.js");
}());
</script>
Basically I used the way described in this answer to only load certain scripts starting from the specific screen resolution. But the third script out of three, that relies on jquery, is not working and says $ is not defined
. Even though if I enter $
in the console, it returns normal response. And when I see the order of the scripts in the HTML output, it is clear that jquery is loaded prior to two other scripts. What could be the issue here?
P.S. everything works comlpetely fine if I just load the scripts normally without the resolution check.