[英]How can I detect Internet Explorer (IE) and Microsoft Edge using JavaScript?
我環顧四周,我知道有很多方法可以檢測 Internet Explorer。
我的問題是:我的 HTML 文檔上有一個區域,單擊該區域時會調用與任何類型的 Internet Explorer 不兼容的 JavaScript 函數。 我想檢測是否正在使用 IE,如果是,請將變量設置為 true。
問題是,我正在用 Notepad++ 編寫代碼,當我在瀏覽器中運行 HTML 代碼時,檢測 IE 的方法都不起作用。 我認為問題是我正在用 Notepad++ 運行它。 我需要能夠檢測 IE,以便根據變量,我可以禁用該站點的該區域。 我試過這個:
var isIE10 = false;
if (navigator.userAgent.indexOf("MSIE 10") > -1) {
// this is internet explorer 10
isIE10 = true;
window.alert(isIE10);
}
var isIE = (navigator.userAgent.indexOf("MSIE") != -1);
if(isIE){
if(!isIE10){
window.location = 'pages/core/ie.htm';
}
}
但它不起作用。 如何從 Notepad++ 中檢測 IE? 這就是我用來測試 HTML 的內容,但我需要一種可以處理它的方法。
我注意到有人將此標記為重復,這是可以理解的。 我想我沒有說清楚。 我不能使用 JQuery 答案,所以這不是重復的,因為我要求的是普通的 JS 答案。
還有一種方法可以檢測 Microsoft Edge 瀏覽器嗎?
這是我知道如何檢查 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 or 11
window.location = 'pages/core/ie.htm';
}
if (/Edge\/\d./i.test(navigator.userAgent)){
// This is Microsoft Edge
window.alert('Microsoft Edge');
}
請注意,您的代碼中不需要額外的 var isIE10,因為它現在會進行非常具體的檢查。
另請查看此頁面以獲取最新的 IE 和 Edge 用戶代理字符串,因為此答案在某些時候可能會過時: https : //msdn.microsoft.com/en-us/library/hh869301%28v=vs.85%29。 aspx
// detect IE8 and above, and Edge
if (document.documentMode || /Edge/.test(navigator.userAgent)) {
... do something
}
解釋:
document.documentMode
一個僅限 IE 的屬性,首先在 IE8 中可用。
/Edge/
用於搜索字符串“Edge”的正則表達式 - 然后我們針對“navigator.userAgent”屬性進行測試
2020 年 3 月更新
@Jam 評論說,最新版本的 Edge 現在將Edg
報告為用戶代理。 所以檢查將是:
if (document.documentMode || /Edge/.test(navigator.userAgent) || /Edg/.test(navigator.userAgent)) {
... do something
}
我不知道為什么,但我沒有像其他人談論的那樣在 userAgent 中看到“Edge”,所以我不得不采取另一條可能對某些人有所幫助的路線。
我沒有查看 navigator.userAgent,而是查看 navigator.appName 來區分它是 IE<=10 還是 IE11 和 Edge。 IE11 和 Edge 使用“Netscape”的 appName,而其他所有迭代都使用“Microsoft Internet Explorer”。
在我們確定瀏覽器是 IE11 或 Edge 之后,我查看了 navigator.appVersion。 我注意到在 IE11 中,字符串很長,里面有很多信息。 我隨意挑出了“三叉戟”這個詞,Edge 的 navigator.appVersion 中絕對沒有這個詞。 測試這個詞讓我能夠區分這兩者。
下面是一個函數,它將返回用戶使用的 Internet Explorer 的數值。 如果在 Microsoft Edge 上,則返回數字 12。
祝你好運,我希望這會有所幫助!
function Check_Version(){
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer'){
var ua = navigator.userAgent,
re = new RegExp("MSIE ([0-9]{1,}[\\.0-9]{0,})");
if (re.exec(ua) !== null){
rv = parseFloat( RegExp.$1 );
}
}
else if(navigator.appName == "Netscape"){
/// in IE 11 the navigator.appVersion says 'trident'
/// in Edge the navigator.appVersion does not say trident
if(navigator.appVersion.indexOf('Trident') === -1) rv = 12;
else rv = 11;
}
return rv;
}
我正在使用 UAParser https://github.com/faisalman/ua-parser-js
var a = new UAParser();
var name = a.getResult().browser.name;
var version = a.getResult().browser.version;
話題有點老,但由於這里的腳本將 Firefox 檢測為誤報(EDGE v12),這是我使用的版本:
function isIEorEDGE(){
if (navigator.appName == 'Microsoft Internet Explorer'){
return true; // IE
}
else if(navigator.appName == "Netscape"){
return navigator.userAgent.indexOf('.NET') > -1; // Only Edge uses .NET libraries
}
return false;
}
這當然可以用更簡潔的方式編寫:
function isIEorEDGE(){
return navigator.appName == 'Microsoft Internet Explorer' || (navigator.appName == "Netscape" && navigator.userAgent.indexOf('.NET') > -1);
}
這個功能非常適合我。 它也檢測邊緣。
最初來自這個 Codepen:
https://codepen.io/gapcode/pen/vEJNZN
/**
* detect IE
* returns version of IE or false, if browser is not Internet Explorer
*/
function detectIE() {
var ua = window.navigator.userAgent;
// Test values; Uncomment to check result …
// IE 10
// ua = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)';
// IE 11
// ua = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko';
// Edge 12 (Spartan)
// ua = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0';
// Edge 13
// ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586';
var msie = ua.indexOf('MSIE ');
if (msie > 0) {
// IE 10 or older => return version number
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
}
var trident = ua.indexOf('Trident/');
if (trident > 0) {
// IE 11 => return version number
var rv = ua.indexOf('rv:');
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
}
var edge = ua.indexOf('Edge/');
if (edge > 0) {
// Edge (IE 12+) => return version number
return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
}
// other browser
return false;
}
然后你可以在你的代碼中使用if (detectIE()) { /* do IE stuff */ }
。
如果你只是想給使用 MS 瀏覽器的用戶一個警告什么的,這段代碼應該不錯。
HTML:
<p id="IE">You are not using a microsoft browser</p>
Javascript:
using_ms_browser = navigator.appName == 'Microsoft Internet Explorer' || (navigator.appName == "Netscape" && navigator.appVersion.indexOf('Edge') > -1) || (navigator.appName == "Netscape" && navigator.appVersion.indexOf('Trident') > -1);
if (using_ms_browser == true){
document.getElementById('IE').innerHTML = "You are using a MS browser"
}
感謝@GavinoGrifoni
對我來說更好的是:
var uA = window.navigator.userAgent,
onlyIEorEdge = /msie\s|trident\/|edge\//i.test(uA) && !!( document.uniqueID || window.MSInputMethodContext),
checkVersion = (onlyIEorEdge && +(/(edge\/|rv:|msie\s)([\d.]+)/i.exec(uA)[2])) || NaN;
去運行: http : //output.jsbin.com/solicul/1/ o http://jsfiddle.net/Webnewbie/apa1nvu8/
使用這個片段: var IE = (navigator.userAgent.indexOf("Edge") > -1 || navigator.userAgent.indexOf("Trident/7.0") > -1) ? true : false;
var IE = (navigator.userAgent.indexOf("Edge") > -1 || navigator.userAgent.indexOf("Trident/7.0") > -1) ? true : false;
一行代碼檢測瀏覽器。
如果瀏覽器是IE或Edge,則返回true;
let isIE = /edge|msie\s|trident\//i.test(window.navigator.userAgent)
這是一個檢測 IE10、IE11 和 Edge 的 javascript 類。
為測試目的注入導航器對象。
var DeviceHelper = function (_navigator) {
this.navigator = _navigator || navigator;
};
DeviceHelper.prototype.isIE = function() {
if(!this.navigator.userAgent) {
return false;
}
var IE10 = Boolean(this.navigator.userAgent.match(/(MSIE)/i)),
IE11 = Boolean(this.navigator.userAgent.match(/(Trident)/i));
return IE10 || IE11;
};
DeviceHelper.prototype.isEdge = function() {
return !!this.navigator.userAgent && this.navigator.userAgent.indexOf("Edge") > -1;
};
DeviceHelper.prototype.isMicrosoftBrowser = function() {
return this.isEdge() || this.isIE();
};
如果我們需要檢查 Edge,請繼續
if(navigator.userAgent.indexOf("Edge") > 1 ){
//do something
}
首先,它肯定不是 Notepad++ 問題。 它是你的“字符串匹配問題”
所有 IE 版本中的通用字符串是MSIE在http://www.useragentstring.com/pages/Internet%20Explorer/查看各種 userAgent 字符串
if(navigator.userAgent.indexOf("MSIE") != -1){
alert('I am Internet Explorer!!');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.