0

There are countless questions about this but the answers to all of them are out of date with IE no longer supporting conditional statements, jQuery no longer supporting detection without a migrate plugin etc. This question has been asked before but there are no answers that remain supported in 2020.

The tag is a perfect example of how simple this should be. Surely there must be a way to detect the browser (IE) and then set a DIV with a warning message to visible.

There have been answers in the past but even they aren't supported now. Of course any JS used to detect the browser would need to be compatible with IE.

My first thought was to do something that would detect MSIE and then display a DIV. It seems up for debate as to if you should use feature or browser detection based on everything I've looked through for the last few hours.

The reason I need this message in IE is because the site isn't compatible due to new JS and CSS features. I wouldn't know where to start with this so apologies for not posting any code. All it needs is something like this.

// Detect if Internet Explorer
//   If no do nothing
// If Internet Explorer
//   Set iewarning to visible
.iewarning {
  display: none;
}
<div class="iewarning">Update your browser</div>

I don't know how to go about detecting features, the browser or user agent but this question has to be asked again now that there's no conditional statements or jQuery support. Pure JS is the most ideal solution. Any approaches using that would be appreciated. Thanks in advance.

EDIT If feature detection could be used to make a DIV visible.

if (typeof Promise == "undefined") {
  document.getElementByClass("iewarning").style.display = "block";
}
.iewarning {
  display: none;
}
<div class="iewarning">Please upgrade your browser</div>

That doesn't work though but seems like it could be a more minimal solution.

EDIT Using a query selector instead should work? I'm entirely lost but willing to use feature detection if someone can help me get it working.

if (typeof Promise == "undefined") {
  document.querySelector("div.iewarning").style.display = "block";
}
.iewarning {
  display: none;
}
<div class="iewarning">Please upgrade your browser</div>
  • 2
    Would [this](https://stackoverflow.com/questions/31757852/how-can-i-detect-internet-explorer-ie-and-microsoft-edge-using-javascript) help? – Artur Jun 18 '20 at 01:33
  • refer this link https://stackoverflow.com/a/10047391/4964569 – Hien Nguyen Jun 18 '20 at 01:46
  • Unfortunately all of the answers there rely on alerts not DIVs for messages and aren't fully supported anymore @Artur – learningtoanimate Jun 18 '20 at 01:50
  • Same issue there @HienNguyen, the accepted answer isn't compatible anymore. – learningtoanimate Jun 18 '20 at 01:51
  • As to the code you added, There's no `getElementByClass` method. You're thinking of `getElementsByClassName`, which returns a list, so you'd need to access the first index. Or just use `.querySelector`. –  Jun 18 '20 at 02:17
  • You're right. I'd forgot to specify name. I'm not sure how to use `.querySelector` or where. If you're able to update your answer that'd be appreciated. Am I at least on the right track? – learningtoanimate Jun 18 '20 at 02:19
  • `.querySelector` is a way of selecting an element from the DOM using the same selector syntax that CSS uses. But it won't work in IE 6 and below, but nether will `getElementsByClassName`. You'd be probably better off giving it an ID and using `getElementById` –  Jun 18 '20 at 02:29

3 Answers3

2

OK based on your comments so far here is the most simplfied browser detection approach I could devise using a div in the HTML as you requested.

function isUnsupported (browser) {
    return (browser.name === "MSIE" || browser.name === "IE")
}
var isUnsupported = this.isUnsupported(this.get_browser());
// Uncomment to simulate being in an IE browser.
//var isUnsupported = true;

this.listen("load", window, function() {
  if (isUnsupported) {
    var warning = document.querySelector(".iewarning");
    warning.style.display = 'block';
  }
});
function listen(evnt, elem, func) {
    if (elem.addEventListener)  // W3C DOM
        elem.addEventListener(evnt,func,false);
    else if (elem.attachEvent) { // IE DOM
         var r = elem.attachEvent("on"+evnt, func);
         return r;
    }
    else window.alert('Error: unsupported browser!');
}
function get_browser() {
    var ua = navigator.userAgent, tem, M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
    if (/trident/i.test(M[1])) {
      tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
      return { name: 'IE', version: (tem[1] || '') };
    }
    if (M[1] === 'Chrome') {
      tem = ua.match(/\bOPR\/(\d+)/)
      if (tem != null) { return { name: 'Opera', version: tem[1] }; }
    }
    if (window.navigator.userAgent.indexOf("Edge") > -1) {
      tem = ua.match(/\Edge\/(\d+)/)
      if (tem != null) { return { name: 'Edge', version: tem[1] }; }      
    }
    M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?'];
    if ((tem = ua.match(/version\/(\d+)/i)) != null) { M.splice(1, 1, tem[1]); }
    return {
      name: M[0],
      version: +M[1]
    };
}
.iewarning {
  display: none;
}
<div>Welcome to the Website</div>
<div class="iewarning">IE not supported. Please upgrade your browser.</div>
apena
  • 2,091
  • 12
  • 19
  • When you uncomment `//var isUnsupported = true;` it's fine but actually viewing the page in IE doesn't show the message. I think given feature detection too it's best to just leave this even though it should be so simple when it's something plenty of sites have. – learningtoanimate Jun 18 '20 at 03:02
  • I am not sure what you are talking about. Edge browser? Stackoverflow wont let you run a code snippet in Internet Explorer as they do not support Internet Explorer. I tested the original version of this with vscode live server in Internet Explorer 11 and it worked. – apena Jun 18 '20 at 03:17
  • 1
    I've tried it hosted via a page on a domain actually in IE11 and it didn't. I'm not too sure why. – learningtoanimate Jun 18 '20 at 03:20
  • Yeah I just tested the version you said didnt work in vscode live server and it didnt work for me either. It is not the browser detection portion, rather its most likley that the HTML display portion of the code is not supported by IE, haha. Thats why I used the modal approach I saw on codepen in the first place. I might fix it. – apena Jun 18 '20 at 03:33
  • @learningtoanimate Yeah I found the problem. The way the style was being set to display the warning div was not IE friendly so I fixed it by changing the line `warning.style = 'display: block;'` to `warning.style.display = 'block';`. The snippet has been updated. Thank you. – apena Jun 18 '20 at 03:39
  • Amazing thank you. Definitely seems the best approach knowing that IE won't receive support but people tend to keep other browsers up to date so support issues aren't as likely. – learningtoanimate Jun 18 '20 at 04:13
1

There are countless questions about this but the answers to all of them are out of date with IE no longer supporting conditional statements, jQuery no longer supporting detection without a migrate plugin etc.

They're not out of date, since those features are still no longer supported, and nothing new has arrived to replace them.

This question has been asked before but there are no answers that remain supported in 2020.

There's a reason for that.

The tag is a perfect example of how simple this should be. Surely there must be a way to detect the browser (IE) and then set a DIV with a warning message to visible.

There are ways, but they have varying degree of reliability, and none match what conditional comments provided.

There have been answers in the past but even they aren't supported now.

That's basically your answer right there.

Of course any JS used to detect the browser would need to be compatible with IE.

Would it? If it's not compatible, wouldn't that give you an indicator that the browser doesn't support the features you want to provide?

My first thought was to do something that would detect MSIE and then display a DIV. It seems up for debate as to if you should use feature or browser detection based on everything I've looked through for the last few hours.

Yes, read those debates. If you want to detect features, then detect features. If you actually want to detect browsers, then do so with the user agent.

The reason I need this message in IE is because the site isn't compatible due to new JS and CSS features.

What about other legacy browsers that don't support the features you want? Why do you only want to deal with one specific browser in this situation.

I wouldn't know where to start with this so apologies for not posting any code.

You already had a good start when you read the myriad of other discussions on this topic.

You seem unwilling to listen to any previous discussion and are hoping there's some magic that hasn't yet found its way to the internet.

There isn't.

Here's a summary of some options.

  • IE's legacy features - If the specific versions of IE that you want to detect have conditional comments enabled, then use that feature.
  • User agent sniffing - gives detailed information about the user agent. It can technically be spoofed by the user, but that's very rare, and who cares. If the user wants you to believe they're using a different browser, then let them.
  • Feature detection - If missing features are your concern, then this would seem to be the obvious choice. However, if you load libraries that provide compatibility for some of the missing features, it could get a little complicated. As long as you're aware of the 3rd party code you load, it shouldn't be an issue.
  • Let it break - Why not? Do you think your site is the only one they'll encounter that uses modern features? Most sites take this approach, and it works incredibly well.
  • Ask the user - Kindly request upon entry that the user tell you their browser and version. Say things like "pretty please" and convince them that it's of the utmost importance (lie to them), and you may have some success.
  • Write a virtual psychic library - This would harness the unseen powers of the universe to detect the exact browser and version of the visitor to your site. Seems like a winning solution to me.

You're not the first to want this, but you're sort of acting like this topic hasn't really been explored in full until today. It has. There's nothing new at the moment. When there is, it will be big news, and you'll see it discussed in those old posts.

  • 1
    Your answer isn't correct though because other websites have managed to do this. I'm willing to listen to other approaches but none that work are available. Even if you detected features then showed a DIV that would be more appropriate. – learningtoanimate Jun 18 '20 at 01:54
  • [codepen.io](https://codepen.io/) does it pretty solidly. – apena Jun 18 '20 at 01:57
  • @learningtoanimate: They either use legacy features in those legacy browsers, or they use the other available features. Just because you see a div, doesn't mean they're *not* using the solutions you've been told about countless times. –  Jun 18 '20 at 02:01
  • I'm not against using those features, I just don't understand them and I don't think your attitude is particularly helpful. – learningtoanimate Jun 18 '20 at 02:04
  • @learningtoanimate: Up until this point you've seemed very much against using those features. Unfortunately, that's about as good as you're going to get. My attitude does seem to be helping, since you seem to be getting closer to dealing with the unfortunate reality of the situation. –  Jun 18 '20 at 02:11
  • It's not conducive to a simple working answer though. If it's something that works to achieve the result it seems like it could at least be considered. The last snippet in my question is the closest I've got @slappy so help with that (if salvageable) would be appreciated. – learningtoanimate Jun 18 '20 at 02:14
1

I just wrote a codepen a couple of days ago that uses the approach codepen.io utilizes in production to notify a user when they come to their site with an IE browser. The notification is modal. It's pretty solid.

CSS:

* {
    margin: 0;
    padding: 0;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}
#unsupported-message :last-child {
    margin-bottom: 0px;
}
html, body {
    height: 100%;
    -webkit-overflow-scrolling: touch;
}
body {
    background: #131417;
}
/* @media all and (min-width:831px) */
body {
    position: relative;
    height: auto;
    min-height: 100vh;
}
body {
    color: white;
}
body {
    position: relative;
    overflow-x: hidden;
    overflow-y: auto;
    height: 100vh;
    background: #131417;
}
/* @media all and (min-width:831px) */
body:not(.sidebar-false) {
    padding-left: 188px;
}
html {
    font-family: "Lato", "Lucida Grande", "Lucida Sans Unicode", Tahoma, Sans-Serif;
    line-height: 1.5;
    font-size: 15px;
    font-weight: 400;
}
html {
    overflow-x: hidden;
}

#unsupported-overlay,
#unsupported-modal {
    display: block;
    position: absolute;
    position: fixed;
    left: 0;
    right: 0;
    z-index: 9999;
    background: #000;
    color: #D5D7DE;
}

#unsupported-overlay {
    top: 0;
    bottom: 0;
    opacity: 0.7;
}

#unsupported-modal {
    top: 80px;
    margin: auto;
    width: 90%;
    max-width: 520px;
    max-height: 90%;
    padding: 40px 20px;
    box-shadow: 0 10px 30px #000;
    text-align: center;
    overflow: hidden;
    overflow-y: auto;
    border: solid 7px #ffdd40;
}

#unsupported-message :last-child { margin-bottom: 0; }

#unsupported-message h2 {
    font-family: 'Telefon', Sans-Serif;
    font-size: 34px;
    color: #FFF;
}

#unsupported-message h3 {
    font-size: 20px;
    color: #FFF;
}

body.hide-unsupport { padding-top: 24px; }
body.hide-unsupport #unsupported-message { visibility: hidden; }

#unsupported-banner {
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    background: #ffdd40;
    color: #000;
    font-size: 14px;
    padding: 2px 5px;
    line-height: 1.5;
    text-align: center;
    visibility: visible;
    z-index: 199;
}

#unsupported-banner a {
    color: #000;
    text-decoration: underline;
}

@media (max-width: 800px), (max-height: 500px) {
    #unsupported-message .modal p {
    /* font-size: 12px; */
    line-height: 1.2;
    margin-bottom: 1em;
    }

    #unsupported-modal {
    position: absolute;
    top: 20px;
    }
    #unsupported-message h1 { font-size: 22px; }
    body.hide-unsupport { padding-top: 0px; }
    #unsupported-banner { position: static; }
    #unsupported-banner strong,
    #unsupported-banner u { display: block; }
}

JS:

// dirty vanilla listen method
function listen(evnt, elem, func) {
    if (elem.addEventListener)  // W3C DOM
        elem.addEventListener(evnt,func,false);
    else if (elem.attachEvent) { // IE DOM
         var r = elem.attachEvent("on"+evnt, func);
         return r;
    }
    else window.alert('Error: unsupported browser!');
}
// dirty browser detection but production worthy
function get_browser() {
    var ua = navigator.userAgent, tem, M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
    if (/trident/i.test(M[1])) {
      tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
      return { name: 'IE', version: (tem[1] || '') };
    }
    if (M[1] === 'Chrome') {
      tem = ua.match(/\bOPR\/(\d+)/)
      if (tem != null) { return { name: 'Opera', version: tem[1] }; }
    }
    if (window.navigator.userAgent.indexOf("Edge") > -1) {
      tem = ua.match(/\Edge\/(\d+)/)
      if (tem != null) { return { name: 'Edge', version: tem[1] }; }      
    }
    M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?'];
    if ((tem = ua.match(/version\/(\d+)/i)) != null) { M.splice(1, 1, tem[1]); }
    return {
      name: M[0],
      version: +M[1]
    };
}

function isUnsupported (browser) {
    return (browser.name === "MSIE" || browser.name === "IE")
}

var isUnsupported = this.isUnsupported(this.get_browser());

/* 
  Uncomment the below line of code to simulate
  being in an IE browser.
*/
//var isUnsupported = true;

if (isUnsupported) {
    this.listen("load", window, function() {
        var d1 = document.createElement('div');
        var a1 = document.createElement('a');
        var d2 = document.createElement('div');
        var title = document.createElement('h2');
        var p1 = document.createElement('p');
        d1.id = 'unsupported-message';
        a1.id = 'unsupported-overlay';
        a1.href = '#unsupported-modal';
        d2.id = 'unsupported-modal';
        d2.role = 'alertdialog';
        d2['aria-labelledby'] = 'unsupported-title';
        title.id = 'unsupported-title';
        title.innerHTML = '⚠ Unsupported Browser ⚠';
        d2.appendChild(title);
        d2.innerHTML += "This site does not support Internet Explorer. We generally only support the recent versions of major browsers like Chrome, Firefox, Safari, and Edge."
        d2.appendChild(p1)
        d1.appendChild(a1);
        d1.appendChild(d2)
        document.body.appendChild(d1);
    });
} else {
  var instructions = document.createElement('div')
  instructions.innerHTML = "Uncomment line 45 (//var isUnsupported = true;) in the javascript to simulate being in an unsupported browser."
  document.body.append(instructions)
}
apena
  • 2,091
  • 12
  • 19
  • That is useful but is it something that needs to be so complex? Also, please correct me if I'm wrong but does that not show a popup alert rather than being part of the page? – learningtoanimate Jun 18 '20 at 01:56
  • If you want production worthy code it does not get much more succinct than that. The message is part of the page but it is made to look like a modal popup so the user will go away and come back with a supported browser but you could edit that part to serve your needs. – apena Jun 18 '20 at 02:04
  • Can you please see my question update with feature detection, it seems like it could be a quicker way to do this. – learningtoanimate Jun 18 '20 at 02:05
  • You can't really use `attachEvent` for a blanket `addEventListener` substitute. There needs to be a good bit of patching to make them equivalent. –  Jun 18 '20 at 02:10
  • @slappy it's just for IE users, why not? – apena Jun 18 '20 at 02:24
  • @apena: There are a number of reasons, but probably the most important is that it doesn't set the value of `this` to the element that received the event listener. Using `this` is very common, so handlers that use it would break. There are other incompatibilities too, but that's arguably the most serious. –  Jun 18 '20 at 02:26
  • @learningtoanimate I got my answer down to 45 lines of code in pure vanilla javascript [here](https://codepen.io/ApoloPena/pen/jOWVvvQ?editors=0110). The solution I offer is browser detection. Feature detection can be messy for a number of reasons, but you need to make up your mind as to which you will use, that is up to you of course. – apena Jun 18 '20 at 02:30
  • @slappy The `listen` method is just used once on `load` to send the user away if they have IE. Are you talking about using the `listen' method for more than that? – apena Jun 18 '20 at 02:32
  • I'm still trying to figure out which offers the best support. I do like the shorter version. I just don't know if feature detection will be better for older versions of browsers. A way to combine both and a DIV in the HTML vs being created by the JS would be most ideal. – learningtoanimate Jun 18 '20 at 02:32
  • Oh, sorry. I thought you were providing that as a shim. I didn't look closely enough. –  Jun 18 '20 at 02:32
  • @slappy no worries. – apena Jun 18 '20 at 02:35
  • @learningtoanimate I guess a good way to figure out which way you should go is to analyze how many features you will need to detect and how many new features you will want to implement in the future. If those numbers are low enough and your IE user base is high enough then feature detection might be a better approach. Otherwise keep it simple. – apena Jun 18 '20 at 02:39
  • Browser detection could work best given that it's likely new features won't be supported by old browsers. I'm close to leaving it entirely and just having a site that doesn't function correctly. Your most recent pen was close but a DIV is needed to be made visible within the HTML, not generated because it may need changes which will be much easier when not in JS. – learningtoanimate Jun 18 '20 at 02:43
  • @learningtoanimate I posted another answer with a code snippet that addresses your request. Good luck to you ;) – apena Jun 18 '20 at 03:00