Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use javascript to identify different browsers

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to use javascript to identify different browsers". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how to use javascript to identify different browsers.

Var client = function () {

/ / presentation engine

Var engine = {

Ie: 0

Gecko: 0

Webkit: 0

Khtml: 0

Opera: 0

/ / with a rest version

Ver: null

}

/ / browser

Var browser = {

/ / browsers

Ie: 0

Firefox: 0

Safari: 0

Konq: 0

Opera: 0

Chrome: 0

/ / specific version

Ver: null

}

Return {

Engine: engine

Browser: browser

}

} ()

A private variable, browser, is added to the code to hold the properties of each major browser. Like the engine variable, except for the currently used browser, the value of the other properties will remain at 0; if it is the currently used browser, what is stored in this property is that most browsers are closely related to their rendering engine, so the code that detects the browser in the following example is mixed with the code that detects the rendering engine:

Var ua = navigator.userAgent

If (window.opera) {

Engine.ver = browser.ver = window.opera.version ()

Engine.opera = browser.opera = parseFloat (engine.ver)

} else if (/ AppleWebKit/ (S+) / .test (ua)) {

Engine.ver = RegExp ["$1"]

Engine.webkit = parseFloat (engine.ver)

/ / figure out if it's Chrome or Safari

If (/ Chrome/ (S+) / .test (ua)) {

Browser.ver = RegExp ["$1"]

Browser.chrome = parseFloat (browser.ver)

} else if (/ Version/ (S+) / .test (ua)) {

Browser.ver = RegExp ["$1"]

Browser.safari = parseFloat (browser.ver)

} else {

/ / approximate version

Var safariVersion = 1

If (engine.webkit < 100) {

SafariVersion = 1

} else if (engine.webkit < 312) {

SafariVersion = 1.2

} else if (engine.webkit < 412) {

SafariVersion = 1.3

} else {

SafariVersion = 2

}

Browser.safari = browser.ver = safariVersion

}

} else if (/ KHTML/ (S+) / .test (ua) | | / Konqueror/ ([^;] +) / .test (ua)) {

Engine.ver = browser.ver = RegExp ["$1"]

Engine.khtml = browser.konq = parseFloat (engine.ver)

} else if (/ rv: ([^)] +)) Gecko/d {8} / .test (ua)) {

Engine.ver = RegExp ["$1"]

Engine.gecko = parseFloat (engine.ver)

/ / determine if it's Firefox

If (/ Firefox/ (S+) / .test (ua)) {

Browser.ver = RegExp ["$1"]

Browser.firefox = parseFloat (browser.ver)

}

} else if (/ MSIE ([^;] +) / .test (ua)) {

Engine.ver = browser.ver = RegExp ["$1"]

Engine.ie = browser.ie = parseFloat (engine.ver)

}

For Opera and IE, the value in the browser object is equal to the value in the englne object. For Konqueror, the browserkonq and browser.ver attributes are equal to the engine.khtml and englne.ver attributes, respectively.

To detect Chrome and Safari, we added if statements to the code of the detection engine. When you extract the version number of Chrome, you need to look for the string "Chrome/" and get the numeric value after that string. When you extract the version number of Safari, you need to look for the string "Version/" and get the value that follows it. Since this approach applies only to Safari3 and later, some alternate code is needed to map the version number of WebKit to the version number of Safari.

When testing the version of Firefox, you first need to find the string "Firefox/:" and then extract the numeric value (that is, the version number) after the string. Of course, this will only be done if the rendering engine is identified as Gecko.

With the above code, we can write the following logic:

If (client.engine.webkit) (/ / if it's WebKit)

If (client.browser.chrome) {/ / execute the code for Chrome

} else if (client.browser.safari) {/ / execute the code for Safari

}

} else if (client.engine.gecko) {

If (client.browser.firefox) {/ / execute the code for Firefox

} else {/ / execute code for other Gecko browsers

}

}

At this point, I believe you have a deeper understanding of "how to use javascript to identify different browsers". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report