Load a javascript file and css file depending on user agent

后端 未结 2 1991
一向
一向 2020-12-11 05:36

Just like in the title.

I got two files: one is javascript file and one is css file. And if user-agent is an iPad I want to load those files - but o

2条回答
  •  渐次进展
    2020-12-11 06:30

    if (navigator.userAgent.match(/iPad/i) != null){ // may need changing?
      var js = document.createElement('script');
      js.type = "text/javascript";
      js.src = "/s/jquery.dropkick-1.0.0.js";
    
      var css = document.createElement('link');
      css.type = "text/css";
      css.rel = "stylesheet";
      css.href = "/c/dropkick.css";
    
      var h = document.getElementsByTagName('head')[0];
      h.appendChild(js);
      h.appendChild(css);
    }
    

    Or whatever would be in the User-Agent header for an iPad.

    References:

    • window.navigator.userAgent
    • document.createElement
    • node.appendChild

提交回复
热议问题