Refresh the page and dynamically import the css file

In web pages, due to the cache loading problem of css files, the updated css may not be reflected in time.
Solution: When refreshing the page, dynamically import the tagged css file, the code is as follows:
    //get random number
    var getRandom = function (n) {   
          var random = '';
          for(var i=0;i<n;i++){
                random+=Math.floor(Math.random()*10);
          }
          return random;
     }
     // css refreshes random tags every time
     function createLink(cssURL){
         var head = document.getElementsByTagName('head')[0],
             linkTag = null;
       
      if(!cssURL){
          return false;
      }    
      linkTag = document.createElement('link');
      linkTag.setAttribute('rel','stylesheet');
      linkTag.setAttribute('_group','recm');
      linkTag.setAttribute('type','text/css');
         linkTag.href = cssURL;  
         head.appendChild(linkTag);
     }
     var cssURL = getRandom (8);
     cssURL = "./css/test.css?v=" + cssURL;
     createLink(cssURL);  

    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326453266&siteId=291194637