簡體   English   中英

如何使用javascript檢測IE / Edge?

[英]How to detect IE/Edge using javascript?

我正在嘗試使用javascript根據用戶使用的瀏覽器將某種樣式應用於頁面。 我可以檢測除IE / Edge之外的所有瀏覽器。 在我的代碼片段中,我只是嘗試檢測IE / Edge並應用該樣式。

這是我的代碼:

var bodyStyle = document.querySelector("#bodyArea");
if((navigator.userAgent.indexOf("Edge") != -1 ) || (!!document.documentMode == true ))
{
    alert("asdf");
    bodyStyle.style.paddingTop = "500px";
}
else
{
    bodyStyle.style.paddingTop = "300px";
}

當我在else部分發出警報時,它會給我一個警報,但它對if部分不起作用。 所以當我嘗試檢測IE / Edge時,我認為我的問題正在發生。 或者,如果它位於別處,請告訴我。 如果有人有任何反饋意見,將不勝感激。 提前致謝!

您可以使用此自定義script來檢測IE / Edge:

if (/MSIE 10/i.test(navigator.userAgent)) {
   // this is internet explorer 10
   window.alert('isIE10');
}

if(/MSIE 9/i.test(navigator.userAgent) || /rv:11.0/i.test(navigator.userAgent)){
    // this is internet explorer 9 and 11
    window.location = 'pages/core/ie.htm';
}

if (/Edge\/12./i.test(navigator.userAgent)){
   // this is Microsoft Edge
   window.alert('Microsoft Edge');
}

查看此頁面以獲取最新的IE和Edge用戶代理字符串: https//msdn.microsoft.com/en-us/library/hh869301%28v=vs.85%29.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM