programing tip

JavaScript를 사용하여 Internet Explorer (IE) 및 Microsoft Edge를 검색하려면 어떻게해야합니까?

itbloger 2020. 11. 10. 07:53
반응형

JavaScript를 사용하여 Internet Explorer (IE) 및 Microsoft Edge를 검색하려면 어떻게해야합니까?


나는 많이 둘러 보았고, 인터넷 익스플로러를 감지하는 방법이 많다는 것을 알고 있습니다.

내 문제는 이것이다 : HTML 문서에 클릭하면 어떤 종류의 Internet Explorer 와도 호환되지 않는 JavaScript 함수를 호출하는 영역이 있습니다. IE가 사용 중인지 감지하고, 그렇다면 변수를 true로 설정하고 싶습니다.

문제는 메모장 ++에서 코드를 작성하고 브라우저에서 HTML 코드를 실행할 때 IE 작동을 감지하는 방법이 없다는 것입니다. 문제는 메모장 ++에서 실행하고 있다는 것입니다. 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';
    }
}

하지만 작동하지 않습니다. 메모장 ++에서 IE를 어떻게 감지합니까? 그것이 내가 HTML을 테스트하고있는 것이지만, 그것과 함께 작동 할 방법이 필요합니다.

편집하다

누군가 이것을 중복으로 표시했으며 이해할 수 있습니다. 명확하지 않은 것 같습니다. JQuery 답변을 사용할 수 없으므로 바닐라 JS 답변을 요청하므로 중복되지 않습니다.

편집 # 2

Microsoft Edge 브라우저를 감지하는 방법도 있습니까?


이유는 모르겠지만 다른 사람들이 말하는 것처럼 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에는없는 "Trident"라는 단어를 임의로 선택했습니다. 이 단어에 대한 테스트를 통해 두 가지를 구분할 수있었습니다.

다음은 사용자가 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;          
}

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

IE8에서 처음 사용할 수있는 IE 전용 속성입니다.

/Edge/

문자열 'Edge'를 검색하는 정규식-그런 다음 'navigator.userAgent'속성에 대해 테스트합니다.


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를 False Positive (EDGE v12)로 감지하므로 다음과 같은 버전을 사용합니다.

function isIEorEDGE(){
  if (navigator.appName == 'Microsoft Internet Explorer'){
    return true; // IE
  }
  else if(navigator.appName == "Netscape"){                       
     return navigator.appVersion.indexOf('Edge') > -1; // EDGE
  }       

  return false;
}

물론 더 간결하게 작성할 수 있습니다.

function isIEorEDGE(){
  return navigator.appName == 'Microsoft Internet Explorer' || (navigator.appName == "Netscape" && navigator.appVersion.indexOf('Edge') > -1);
}

이 기능은 저에게 완벽하게 작동했습니다. Edge도 감지합니다.

원래이 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 */ }코드에서 사용할 수 있습니다 .


If you just want to give users using a MS browser a warning or something, this code should be good.

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"
}

Thanks to @GavinoGrifoni


For me better this:

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;

Go run: http://output.jsbin.com/solicul/1/ o http://jsfiddle.net/Webnewbie/apa1nvu8/


Use this snip : var IE = (navigator.userAgent.indexOf("Edge") > -1 || navigator.userAgent.indexOf("Trident/7.0") > -1) ? true : false;


Here is a javascript class that detects IE10, IE11 and Edge.
Navigator object is injected for testing purposes.

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();
};

One line code to detect the browser.

If the browser is IE or Edge, It will return true;

let isIE = /edge|msie\s|trident\//i.test(window.navigator.userAgent)

First of all its not the Notepad++ problem for sure. Its your "String Matching problem"

The common string throughout all IE version is MSIE Check out the various userAgent strings at http://www.useragentstring.com/pages/Internet%20Explorer/

if(navigator.userAgent.indexOf("MSIE") != -1){
   alert('I am Internet Explorer!!');
}

참고URL : https://stackoverflow.com/questions/31757852/how-can-i-detect-internet-explorer-ie-and-microsoft-edge-using-javascript

반응형