Home > Web Front-end > JS Tutorial > js identifies different browsers and makes judgments based on userAgent_javascript skills

js identifies different browsers and makes judgments based on userAgent_javascript skills

WBOY
Release: 2016-05-16 16:41:00
Original
1634 people have browsed it

检测浏览器,注意浏览器判断顺序,主要是基于userAgent做判断。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

//检测浏览器

var client = function(){

var engine = {

ie:0,

gecko:0,

webkit:0,

khtml:0,

opera:0,

ver:null

};

var browser = {

//浏览器

ie: 0,

firefox: 0,

safari: 0,

konq: 0,

opera: 0,

chrome: 0,

ver:null

};

var ua = navigator.userAgent;

//浏览器检测有顺序

if(window.opera){//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);

 

 

//确定是 Chrome 还是 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 {

//近似地确定版本号

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);

//确定是不是 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);

}

return {

engine:engine,

browser: browser

};

}();

 

 

//调用

 

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

if (client.browser.chrome){

//执行针对 Chrome 的代码

} else if (client.browser.safari){

//执行针对 Safari 的代码

}

} else if (client.engine.gecko){

if (client.browser.firefox){

//执行针对 Firefox 的代码

} else {

//执行针对其他 Gecko 浏览器的代码

}

}

Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template