(function() {
var browser = 'other',
system = 'other',
device = 'PC';
var pf = navigator.platform,
ua = navigator.userAgent,
href = window.location.href;
// 埋点地址
var root = '';
// 开关
var ON = false;
// window系统匹配表
var WinMap = {
'windows nt 5.0': 'Win2000',
'windows 2000': 'Win2000',
'windows nt 5.1': 'WinXP',
'windows xp': 'WinXP',
'windows nt 5.2': 'Win2003',
'windows 2003': 'Win2003',
'windows nt 6.0': 'WinVista',
'windows vista': 'WinVista',
'windows nt 6.1': 'Win7',
'windows 7': 'Win7',
'windows nt 6.2': 'Win8',
'windows 8': 'Win8',
'windows nt 6.3': 'Win8.1',
'windows 8.1': 'Win8.1'
};
var LinuxMap = {
'android': 'Android',
'linux': 'Linux'
};
// 各系统匹配表
var sysMap = {
'win32': WinMap,
'windows': WinMap,
'ipad': 'iOS',
'iphone': 'iOS',
'macintosh': 'Mac',
'macIntel': 'Mac',
'mac': 'Mac',
'x11': 'Unix',
'linux': LinuxMap
};
// 合并参数
function buildQuery(obj) {
var i,
arr = [];
if(typeof obj === "object") {
for(i in obj) {
if(obj.hasOwnProperty(i)) {
arr.push(i + "=" + encodeURIComponent(obj[i] == null ? "" : obj[i] + ""));
}
}
}
return arr.join("&");
}
// 发起请求
function create(url) {
var img = new Image(0, 0);
img.onload = function() {
img = null;
};
img.src = url;
if(img.complete) {
img = null;
}
//alert(url);
}
// 初始化操作
function initialize() {
var lua = ua.toLowerCase(),
lpf = pf.toLowerCase(),
match, version;
// 判断操作系统
for(i in sysMap) {
if(sysMap.hasOwnProperty(i) && lpf.indexOf(i) > -1) {
if(typeof sysMap[i] === 'object') {
for(j in sysMap[i]) {
if(sysMap[i].hasOwnProperty(j) && lua.indexOf(j) > -1) {
system = sysMap[i][j];
break;
}
}
} else {
system = sysMap[i];
}
break;
}
}
// 判断设备
if(system === 'Mac') {
device = 'Mac';
}
else if(system === 'iOS') {
match = /iPad|iPhone/.exec(ua);
device = match && match[0] || device;
}
else if(system === 'Android') {
device = 'Mobile';
}
// 判断浏览器
match = /(chrome)[ \/]([\w.]+)/.exec(lua) ||
/(webkit)[ \/]([\w.]+)/.exec(lua) ||
/ms(ie)\s([\w.]+)/.exec(lua) ||
/(firefox)[ \/]([\w.]+)/.exec(lua) ||
[];
if(match && match[1]) {
if(match[1] === 'ie') {
// ie判断版本号
version = /msie\s([\d\.]+)/.exec(lua);
browser = match[1] + (version && version[1] ? parseInt(version[1]) : '');
}
else if(match[1] === 'webkit') {
browser = 'Webkit';
// webkit内核类 主要是移动端
if(lua.indexOf('safari') > -1 && (system === 'iOS' || system === 'MAC')) {
browser = 'Safari';
}
}
else {
// 默认 首字母大写
browser = match[1].substr(0, 1).toUpperCase() + match[1].substr(1);
}
}
}
initialize();
// 埋点
function infoc(obj) {
if(ON === false) {
return;
}
var i;
if(typeof obj === "object") {
for(i in infoc.params) {
if(infoc.params.hasOwnProperty(i)) {
if(typeof obj[i] === "undefined") {
obj[i] = infoc.params[i];
}
}
}
}
setTimeout(function () {
create(root + buildQuery(obj));
}, 1);
}
// 开关
infoc.on = function() {
ON = true;
return this;
};
infoc.off = function() {
ON = false;
return this;
};
// 公共参数
infoc.params = {
product_no: 0,
public_index: 0,
business_index: 0
};
infoc.addParams = function(obj) {
if(typeof obj === "object") {
for(i in obj) {
if(obj.hasOwnProperty(i)) {
if(typeof i === "string" || typeof i === "number") {
this.params[i] = obj[i];
}
}
}
}
return this;
};
infoc.getDevice = function() {
return {
browser: browser,
system: system,
device: device
};
};
infoc.getURL = function () {
return href;
};
//获得cookie值
infoc.getCookie = function (objName) {
var arrStr = document.cookie.split("; ");
for (var i = 0; i < arrStr.length; i++) {
var temp = arrStr[i].split("=");
if (temp[0] == objName) return unescape(temp[1]);
}
return "";
}
//添加cookie
infoc.setCookie = function (objName, objValue, objHours) {
var str = objName + "=" + escape(objValue);
if (objHours > 0) {//为0时不设定过期时间,浏览器关闭时cookie自动消失
var date = new Date();
var ms = objHours * 3600 * 1000;
date.setTime(date.getTime() + ms);
str += "; expires=" + date.toGMTString();
}
document.cookie = str;
return this;
}
//删除cookie
infoc.delCookie = function (keyName) {
var date = new Date();
date.setTime(date.getTime() - 10000);
document.cookie = name + "=a; expires=" + date.toGMTString();
return this;
}
//获得参数
infoc.getQueryStringValue = function (keyName) {
var searchStr = location.search.substr(1);
if (searchStr.length == 0)
return null;
var collection = searchStr.split('&');
for (var i = 0; i < collection.length; i++) {
var tmp = collection[i].split('=');
if (tmp.length < 2)
continue;
if (tmp[0].toUpperCase() == keyName.toUpperCase())
return tmp[1];
}
return null;
}
//设置frm
infoc.setFrm = function (obj) {
infoc.setCookie("Infoc.Frm", obj, 2);
return this;
}
//得到frm
infoc.getFrm = function () {
return infoc.getCookie("Infoc.Frm");
}
window.infoc = infoc;
})();
// 公共字段设置 奖多多
infoc.addParams({
product_no: 37,
public_index: 1,
business_index: 101,
browser: infoc.getDevice().browser,
os: infoc.getDevice().system,
uid: infoc.getCookie("LY.CP.JDD.User")
});
如果您觉得本文的内容对您的学习有所帮助:
关键字:
jquery