How to load or not a JS script (into the head section) based on screen resolution?

前端 未结 6 1384
眼角桃花
眼角桃花 2020-12-20 02:36

on a website, I have these lines on the tag

  
  

        
6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-20 03:10

    In your script

     var width = (window.innerWidth > 0) ? window.innerWidth : screen.width;
     var head = document.getElementsByTagName('head').item(0);
     if(width > 950){
           var script = document.createElement("script");
           script.type = "text/javascript";
           script.src = "http://www.domain.com/js/jquery.min.js";
           head.appendChild(script);
        }
    

提交回复
热议问题