JavaScript determines whether the browser is IE

JavaScript determines whether the browser is IE

As a front-end developer, I can’t avoid IE’s pitfalls. Other browsers are fine, but IE is broken. There is no support for various things. I’m convinced.

Some properties and methods are not supported by all versions of IE, while some are partially supported. In the project, the main dividing line is IE8. I believe that most of the projects currently under maintenance and development support IE8 and above. So this article briefly summarizes how to determine whether the browser is IE and its version is 8.0.

First of all, some properties and methods are not supported by all versions of IE, so you only need to determine whether it is IE

The following three are the methods I used in my project. If there are new methods, they will be updated. If you have other better methods, please feel free to share them~~

  • document.all
  • window.ActiveXObject
  • window.navigator.msSaveOrOpenBlob
//Choose one function isIE(){
 // It is said that Firefox will add the document.all method in the future, so it is recommended to use the other two methods if (document.all) return true; 
 
 if (!!window.ActiveXObject || "ActiveXObject" in window) return true; 
 
 if (window.navigator && window.navigator.msSaveOrOpenBlob) return true; 
}

Determine if the browser is IE8 or below

As I mentioned above, most of the projects under maintenance and development only support IE8 and above.

navigator.userAgent

function isIE8(){
 var DEFAULT_VERSION = 8.0; 
 var ua = navigator.userAgent.toLowerCase(); 
 var isIE = ua.indexOf("msie")>-1; 
 var safariVersion; 
 if(isIE){ 
  safariVersion = ua.match(/msie ([\d.]+)/)[1]; 
 } 
 if (safariVersion <= DEFAULT_VERSION ) { 
  return true 
 };
}

If you have special requirements and need to be compatible with lower versions, then:

var isIE = !!window.ActiveXObject; 

var isIE6 = isIE && !window.XMLHttpRequest; 

var isIE8 = isIE && !!document.documentMode; 

var isIE7 = isIE && !isIE6 && !isIE8;

CSS properties not supported by IE8 and below

  • box-shadow
  • linear-gradient
  • Prompt placeholder
  • Transparency rgba
  • border-image
  • border-radius
  • Rotation-related transform

Methods not supported by IE

Browse PDF files online. Since IE does not have a built-in PDF reader, you can only download and view them.

  • When the backend returns a file stream: window.navigator.msSaveOrOpenBlob(blob); For details, please refer to the other two blog posts.
  • JavaScript processes the PDF file stream returned by the backend, and previews and downloads the PDF file online
  • Vue.js +pdf.js processes the response pdf file stream data, and the front-end image preview cannot be downloaded

The download attribute of the <a> tag is not supported, so you can create a new iframe and set its src attribute

if (isIE()){
	$("a").bind('click',function(){
		var elemIF = document.createElement("iframe"); 
		elemIF.src = FilePath;
		elemIF.style.display = "none"; 
		document.body.appendChild(elemIF);
	});
} else {
	$("a").attr("href",FilePath).attr("download",FileName);
}

The above is the details of JavaScript judging whether the browser is IE. For more information about JavaScript judging browser, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Differences between this keyword in NodeJS and browsers
  • Example code for JavaScript to realize automatic scrolling and clicking of browser web pages
  • How to use the webcam in your browser using JavaScript
  • How to use JavaScript to manipulate the browser history API
  • Detailed explanation of JS cross-browser XML application process
  • Mobile browser invokes WeChat sharing (JS)
  • The browser JavaScript debugging function cannot be used. Solution
  • Summary of commonly used JavaScript tool functions (browser environment)
  • Example of judging browser type based on js
  • How to detect if the current browser is a headless browser with JavaScript

<<:  How to use Docker container to access host network

>>:  Summary of Mysql slow query operations

Recommend

Apply provide and inject to refresh Vue page method

Table of contents Method 1: Call the function dir...

MySQL controls the number of attempts to enter incorrect passwords

1. How to monitor MySQL deadlocks in production e...

Analysis of Apache's common virtual host configuration methods

1. Apache server installation and configuration y...

jQuery treeview tree structure application

This article example shares the application code ...

Introduction to the use and advantages and disadvantages of MySQL triggers

Table of contents Preface 1. Trigger Overview 2. ...

Sample code for deploying ELK using Docker-compose

environment Host IP 192.168.0.9 Docker version 19...

CocosCreator Skeleton Animation Dragon Bones

CocosCreator version 2.3.4 Dragon bone animation ...

How to create dynamic QML objects in JavaScript

1. Dynamically create objects There are two ways ...

How to add fields to a large data table in MySQL

Preface I believe everyone is familiar with addin...

CentOS 8 Installation Guide for Zabbix 4.4

Zabbix server environment platform ZABBIX version...

Implementation example of Docker rocketmq deployment

Table of contents Preparation Deployment process ...