Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to use Modernizr to detect IE?

I wanted to use the Modernizr JS library to detect for some browser properties to determine what content to show or not show.

I have an app called Pano2VR which outputs both HTML5 and SWF. I need the HTML5 for iOS device users.

However, IE does not render this "HTML5" output at all. It seems their output uses CSS3 3D transforms and WebGL, one or more apparently unsupported in IE9.

So, for those users I need to display the Flash version. I was planning to use an IFRAME and either pass the SRC via a Modernizr script or document.write out the correct IFRAME code depending on browser.

All of which leads to how do I use Modernizr to detect simply IE or not IE? Or detect for CSS 3d transforms?

Or is there another way to do this?

like image 911
Steve Avatar asked Nov 20 '12 17:11

Steve


People also ask

How does modernizr detect browser?

We can access various properties of this object 'Modernizr' for feature detection using “Modernizr. featureName”. For example, Modernizr. video will return “true” if the browser supports the video element, and false if the browser doesn't.

How does Modernizr work?

Modernizr is a JavaScript library that detects which HTML5 and CSS3 features your visitor's browser supports. In detecting feature support, it allows developers to test for some of the new technologies and then provide fallbacks for browsers that do not support them.

How you can use modernizr in HTML5?

Modernizr provides an easy way to detect any new feature so that you can take corresponding action. For example, if a browser does not support video feature then you would like to display a simple page.


2 Answers

Modernizr doesn't detect browsers as such, it detects which feature and capability are present and this is the whole jist of what it's trying to do.

You could try hooking in a simple detection script like this and then using it to make your choice. I've included Version Detection as well just in case that's needed. If you only want to check of any version of IE you could just look for the navigator.userAgent having a value of "MSIE".

var BrowserDetect = {          init: function () {              this.browser = this.searchString(this.dataBrowser) || "Other";              this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "Unknown";          },          searchString: function (data) {              for (var i = 0; i < data.length; i++) {                  var dataString = data[i].string;                  this.versionSearchString = data[i].subString;                    if (dataString.indexOf(data[i].subString) !== -1) {                      return data[i].identity;                  }              }          },          searchVersion: function (dataString) {              var index = dataString.indexOf(this.versionSearchString);              if (index === -1) {                  return;              }                var rv = dataString.indexOf("rv:");              if (this.versionSearchString === "Trident" && rv !== -1) {                  return parseFloat(dataString.substring(rv + 3));              } else {                  return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));              }          },            dataBrowser: [              {string: navigator.userAgent, subString: "Edge", identity: "MS Edge"},              {string: navigator.userAgent, subString: "MSIE", identity: "Explorer"},              {string: navigator.userAgent, subString: "Trident", identity: "Explorer"},              {string: navigator.userAgent, subString: "Firefox", identity: "Firefox"},              {string: navigator.userAgent, subString: "Opera", identity: "Opera"},                {string: navigator.userAgent, subString: "OPR", identity: "Opera"},                  {string: navigator.userAgent, subString: "Chrome", identity: "Chrome"},               {string: navigator.userAgent, subString: "Safari", identity: "Safari"}                 ]      };            BrowserDetect.init();      document.write("You are using <b>" + BrowserDetect.browser + "</b> with version <b>" + BrowserDetect.version + "</b>");

You can then simply check for:

BrowserDetect.browser == 'Explorer'; BrowserDetect.version <= 9; 
like image 144
Code Uniquely Avatar answered Oct 13 '22 06:10

Code Uniquely


You can use Modernizr to detect simply IE or not IE, by checking for SVG SMIL animation support.

If you've included SMIL feature detection in your Modernizr setup, you can use a simple CSS approach, and target the .no-smil class that Modernizr applies to the html element:

html.no-smil {   /* IE/Edge specific styles go here - hide HTML5 content and show Flash content */ } 

Alternatively, you could use Modernizr with a simple JavaScript approach, like so:

if ( Modernizr.smil ) {   /* set HTML5 content */ } else {   /* set IE/Edge/Flash content */ } 

Bear in mind, IE/Edge might someday support SMIL, but there are currently no plans to do so.

For reference, here's a link to the SMIL compatibility chart at caniuse.com.

like image 26
jacefarm Avatar answered Oct 13 '22 05:10

jacefarm