Show a message if the browser is not internet explorer 9 or greater

后端 未结 10 1350
北恋
北恋 2020-12-31 01:18

I would like to show my users a bar that looks like this, if:

  1. Browser is not IE; or
  2. Browser is IE but is version 8 or earlier

10条回答
  •  情深已故
    2020-12-31 01:32

    EDIT: This directly answers the OP.

    I have updated Dany's answer with two updates tested in (IE 6,7,8,9,10,11), Chrome, and Edge. Primarily because the updates are very hard to read in the comments.

    • Pure javascript - No jQuery required
    • IE10 reports IE 10 vs IE 1
    • This now reports Edge
    • No specific HTML elements required to pre-exist (other than a body)
    • Tested in IE6, IE7, IE8, IE9, IE11, Chrome v62, and Edge
    • TODO: get it working properly in OSX Sierra, and iPhone

    The test for edge must be first as it claims to be everything. :/

    All this being said Browser detection "is what it is" and we can hope that the need for it will go away soon.

    browser = {};
    if (/(Edge\/[0-9]{2})/i.test(navigator.userAgent)) {
        browser.agent = navigator.userAgent.match(/(Edge\/[0-9]{2})/i)[0].split("/")[0];
        browser.version = parseInt(navigator.userAgent.match(/(Edge\/[0-9]{2})/i)[0].split("/")[1]);
    } else if (/(chrome\/[0-9]{2})/i.test(navigator.userAgent)) {
        browser.agent = navigator.userAgent.match(/(chrome\/[0-9]{2})/i)[0].split("/")[0];
        browser.version = parseInt(navigator.userAgent.match(/(chrome\/[0-9]{2})/i)[0].split("/")[1]);
    } else if (/(firefox\/[0-9]{2})/i.test(navigator.userAgent)) {
        browser.agent = navigator.userAgent.match(/(firefox\/[0-9]{2})/i)[0].split("/")[0];
        browser.version = parseInt(navigator.userAgent.match(/(firefox\/[0-9]{2})/i)[0].split("/")[1]);
    } else if (/(MSIE\ [0-9]{1})/i.test(navigator.userAgent)) {
        browser.agent = navigator.userAgent.match(/(MSIE\ [0-9]{1})/i)[0].split(" ")[0];
        browser.version = parseInt(navigator.userAgent.match(/(MSIE\ [0-9]+)/i)[0].split(" ")[1]);
    } else if (/(Opera\/[0-9]{1})/i.test(navigator.userAgent)) {
        browser.agent = navigator.userAgent.match(/(Opera\/[0-9]{1})/i)[0].split("/")[0];
        browser.version = parseInt(navigator.userAgent.match(/(Opera\/[0-9]{1})/i)[0].split("/")[1]);
    } else if (/(Trident\/[7]{1})/i.test(navigator.userAgent)) {
        browser.agent = "MSIE";
        browser.version = 11;
    } else {
        browser.agent = false;
        browser.version = false;
    }
    
    if (/(Windows\ NT\ [0-9]{1}\.[0-9]{1})/.test(navigator.userAgent)) {
        browser.os = "Windows";
    
        switch (parseFloat(navigator.userAgent.match(/(Windows\ NT\ [0-9]{1}\.[0-9]{1})/)[0].split(" ")[2])) {
            case 6.0:
                browser.osversion = "Vista";
                break;
            case 6.1:
                browser.osversion = "7";
                break;
            case 6.2:
                browser.osversion = "8";
                break;
            default:
                browser.osversion = false;
        }
    } else if (/(OS\ X\ [0-9]{2}\.[0-9]{1})/.test(navigator.userAgent)) {
        browser.os = "OS X";
        browser.osversion = navigator.userAgent.match(/(OS\ X\ [0-9]{2}\.[0-9]{1})/)[0].split(" ")[2];
    } else if (/(Linux)/.test(navigator.userAgent)) {
        browser.os = "Linux";
        browser.osversion = false;
    }
    
    if (browser.agent === "MSIE" && browser.version <= 9) {
        var newDiv = document.createElement("div");
        newDiv.innerHTML = "IE9 is not supported. You are using an UNSUPPORTED version of Internet Explorer.";
        newDiv.setAttribute("style", "background-color:yellow;padding:18px;");
        document.body.insertBefore(newDiv, document.body.firstChild);
    } else { //TODO: Remove for Prod only added to show some flexibility and testing 
        var newDiv = document.createElement("div");
        newDiv.innerHTML = "" + browser.agent + " is so supported. You are using version: " + browser.version + ".";
        newDiv.setAttribute("style", "background-color:cyan;padding:12px;");
        document.body.insertBefore(newDiv, document.body.firstChild);
    }
    

提交回复
热议问题