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

How to specify parameter variables externally in docker

This article mainly introduces how to specify par...

Detailed explanation of the usage and differences of MySQL views and indexes

MySQL Views Simply put, a MySQL view is a shortcu...

CentOS method to modify the default ssh port number example

The default ssh port number of Linux servers is g...

Example of implementing element table row and column dragging

The element ui table does not have a built-in dra...

Example of creating circular scrolling progress bar animation using CSS3

theme Today I will teach you how to create a circ...

Some parameter descriptions of text input boxes in web design

<br />In general guestbooks, forums and othe...

How to choose the right MySQL datetime type to store your time

When building a database and writing a program, i...

CentOS7 firewall and port related commands introduction

Table of contents 1. Check the current status of ...

Solve the error of installing VMware Tools on Ubuntu 18.04

1. According to the online tutorial, the installa...

Methods and steps for Etcd distributed deployment based on Docker

1. Environmental Preparation 1.1 Basic Environmen...

Detailed explanation of the basic commands of Firewalld firewall in Centos7

1. Basics of Linux Firewall The Linux firewall sy...

Summary of commonly used CSS encapsulation methods

1. pc-reset PC style initialization /* normalize....

JavaScript pre-analysis, object details

Table of contents 1. Pre-analysis 1. Variable pre...

Detailed explanation of several ways to create a top-left triangle in CSS

Today we will introduce several ways to use CSS t...