判斷不同瀏覽器,加載不同的css和js文件


在低版本的IE中,條件注釋還有效果,但是在ie9,10,11瀏覽器中,條件注釋不起作用。

在網上找了個校驗ie的方法。

function isIE(){
  if (window.ActiveXObject || "ActiveXObject" in window){
    alert('is ie')
  }else{
    alert(not ie')
  }
}
isIE();


IE下才有這個對象ActiveXObject

當然我們也可以根據瀏覽器是否為IE,引入不同的js和css文件。

這里網上有個例子。

var linkNode = document.createElement("link"),scriptNode = document.createElement("script");  
linkNode.setAttribute("rel","stylesheet");  
linkNode.setAttribute("type","text/css");  
scriptNode.setAttribute("type", "text/javascript");  

if(getBrowser()=="IE"){  
    linkNode.setAttribute("href","css/index-ie.css");  
    scriptNode.setAttribute("src", "js/index-ie.js");  
}else if(getBrowser()=="Firefox"){  
    linkNode.setAttribute("href","css/index-firefox.css");  
    scriptNode.setAttribute("src", "js/index-firefox.js");  
}else if(getBrowser()=="Opera"){  
    linkNode.setAttribute("href","css/index-opera.css");  
    scriptNode.setAttribute("src", "js/index-opera.js");  
}else if(getBrowser()=="Chrome"){  
    linkNode.setAttribute("href","css/index-chrome.css");  
    scriptNode.setAttribute("src", "js/index-chrome.js");  
}else if(getBrowser()=="Safari"){  
    linkNode.setAttribute("href","css/index-safari.css");  
    scriptNode.setAttribute("src", "js/index-safari.js");  
}  
document.head.appendChild(linkNode);  
document.head.appendChild(scriptNode);

即先生成link和script標簽,給類型,再根據瀏覽器的不同給出不同的路徑
是ie就引入polyfill.js和browser.js,並且vue的script標簽類型為text/babel,非IE瀏覽器下,不需要引入polyfill.js和browser.js,vue的script標簽類型為text/javascript,即最傳統的類型。

function isIE(src){
  var polyfill = document.createElement("script");
  var browser = document.createElement("script");
  var myVuejs = document.createElement("script");
  polyfill.setAttribute("src", "../../js/bootshop/js/polyfill.js");
  browser.setAttribute("src", "../../js/bootshop/js/browser.min.js");
  myVuejs.setAttribute("src", src);

  if (window.ActiveXObject || "ActiveXObject" in window){
    myVuejs.setAttribute("type", "text/babel");
    document.head.appendChild(polyfill);
    document.head.appendChild(browser);
    document.head.appendChild(myVuejs);
  }else{
    document.head.appendChild(myVuejs);
  }
}

原文:https://blog.csdn.net/cofecode/article/details/80175179


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM