Replace script when viewed in IE9?

How easy would it be to run another javascript depending on the user's browser?

For example, if someone visits a site with IE9, I would like him to run another script, the one he usually ran for Chrome users.

+4
source share
4 answers

You can use conditional comments for this:

<!--[if lte IE 6]> <script>alert('lte stands for less than or equal to')</script> <![endif]--> <!--[if lt IE 7]> <script>alert('lt stands for less than')</script> <![endif]--> <!--[if gte IE 9]> <script>alert('gte stands for greater than or equal to')</script> <![endif]--> 

and etc.

+3
source

There are various methods for detecting the browser. You can try to discover the browser (by yourself by exploring the navigator.userAgent property), or you can use the methods available in almost every JavaScript structure. For instance:

Alternatively, you can use conditional comments , but they are recognized only IE.

- Pavel

+1
source

You can use conditional comments in HTML.

 <!--[if IE]> <script src="ie_only.js"></script> <![endif]--> 

It can also be used for external style sheets.

Additional information on this post http://css-tricks.com/how-to-create-an-ie-only-stylesheet/

0
source

May I recommend feature detection.

 if (someIEAPI) { // ie code } else if (someChromeAPI) { // chrome code } 

This code is more reliable, and if firefox or opera supports one of the APIs, you still do not need to check their browsers.

In addition, browsers have a habit of lying to you and pretending to be hips.

0
source

Source: https://habr.com/ru/post/1334371/


All Articles