space.website.visit.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //设备信息
  2. var getDeviceInfo = function (key) {
  3. var agent = navigator.userAgent.toLowerCase()
  4. //获取版本号
  5. , getVersion = function (label) {
  6. var exp = new RegExp(label + '/([^\\s\\_\\-]+)');
  7. label = (agent.match(exp) || [])[1];
  8. return label || false;
  9. }
  10. //返回结果集
  11. , result = {
  12. os: function () { //底层操作系统
  13. if (/windows/.test(agent)) {
  14. return 'windows';
  15. } else if (/linux/.test(agent)) {
  16. return 'linux';
  17. } else if (/iphone|ipod|ipad|ios/.test(agent)) {
  18. return 'ios';
  19. } else if (/mac/.test(agent)) {
  20. return 'mac';
  21. }
  22. }()
  23. , ie: function () { //ie版本
  24. return (!!window.ActiveXObject || "ActiveXObject" in window) ? (
  25. (agent.match(/msie\s(\d+)/) || [])[1] || '11' //由于ie11并没有msie的标识
  26. ) : false;
  27. }()
  28. , weixin: getVersion('micromessenger') //是否微信
  29. };
  30. //任意的key
  31. if (key && !result[key]) {
  32. result[key] = getVersion(key);
  33. }
  34. //移动设备
  35. result.android = /android/.test(agent);
  36. result.ios = result.os === 'ios';
  37. result.mobile = (result.android || result.ios) ? true : false;
  38. return result;
  39. };
  40. // 浏览器信息
  41. var getBrowserInfo = function () {
  42. var agent = navigator.userAgent.toLowerCase();
  43. var regStr_ie = /msie [\d.]+;/gi;
  44. var regStr_ff = /firefox\/[\d.]+/gi
  45. var regStr_chrome = /chrome\/[\d.]+/gi;
  46. var regStr_saf = /safari\/[\d.]+/gi;
  47. //IE
  48. if (agent.indexOf("msie") > 0) {
  49. return agent.match(regStr_ie);
  50. }
  51. //firefox
  52. if (agent.indexOf("firefox") > 0) {
  53. return agent.match(regStr_ff);
  54. }
  55. //Safari
  56. if (agent.indexOf("safari") > 0 && agent.indexOf("chrome") < 0) {
  57. return agent.match(regStr_saf);
  58. }
  59. //Chrome
  60. if (agent.indexOf("chrome") > 0) {
  61. return agent.match(regStr_chrome);
  62. }
  63. }
  64. var getClientInfo = function() {
  65. try {
  66. var deviceInfo = getDeviceInfo();
  67. var browserInfo = getBrowserInfo();
  68. deviceInfo.browserInfo = (browserInfo && browserInfo[0]) ? browserInfo[0] : 'unknown';
  69. return encodeURI(JSON.stringify(deviceInfo));
  70. } catch (error) {
  71. return "GetClientInfo Error! msg: " + error;
  72. }
  73. }
  74. ;(function(){
  75. // 访问统计接口返回 HTML。使用隐藏 iframe 触发服务端记录,避免 script MIME 报错影响页面控制台。
  76. var bp = document.createElement('iframe');
  77. bp.style.cssText = 'display:none;width:0;height:0;border:0';
  78. bp.width = '0';
  79. bp.height = '0';
  80. bp.tabIndex = -1;
  81. bp.setAttribute('aria-hidden', 'true');
  82. bp.src = '/website/visit/?address=' + encodeURI(location.href) + "&pathname=" + encodeURI(location.pathname) + "&msg=" + getClientInfo();
  83. if (document.body) {
  84. document.body.appendChild(bp);
  85. } else {
  86. var s = document.getElementsByTagName("script")[0];
  87. s.parentNode.insertBefore(bp, s);
  88. }
  89. })();