1、获取访问的终端
//获取访问的终端 function terminal(){ var system ={}; var p = navigator.platform; var terminal=null; system.win = p.indexOf("Win") == 0; system.mac = p.indexOf("Mac") == 0; system.x11 = (p == "X11") || (p.indexOf("Linux") == 0); if(system.win||system.mac||system.xll){ terminal="电脑端" }else{ terminal="移动端" } return terminal; }
2、获取操作系统
//获取操作系统 function getOSInfo(){ var ua = navigator.userAgent.toLowerCase(); var osType = null; if(ua.indexOf("win") >= 0){ osType = "Windows"; }else if(ua.indexOf("mac") >= 0){ osType = "Mac OS X"; }else if(ua.indexOf("x11") >= 0){ osType = "Unix"; }else if(ua.indexOf("android") >= 0){ osType = "Android"; }else if(ua.indexOf("iphone") >= 0){ osType = "iPhone OS"; }else{ osType = "Unknow"; } return osType; }
3、获取浏览器的类型
//获取浏览器的类型 function getBrowser(){ var explorer = window.navigator.userAgent; if (explorer.indexOf("QQBrowser") >= 0 || explorer.indexOf("QQ") >= 0) { return myexplorer = "腾讯QQ"; } else if (explorer.indexOf("Safari") >= 0 && explorer.indexOf("MetaSr") >= 0) { return myexplorer = "搜狗"; } else if (!!window.ActiveXObject || "ActiveXObject" in window) {//IE if (!window.XMLHttpRequest) { return myexplorer = "IE6"; } else if (window.XMLHttpRequest && !document.documentMode) { return myexplorer = "IE7"; } else if (!-[ 1, ] && document.documentMode && !("msDoNotTrack" in window.navigator)) { return myexplorer = "IE8"; } else {//IE9 10 11 var hasStrictMode = (function() { "use strict"; return this === undefined; }()); if (hasStrictMode) { if (!!window.attachEvent) { return myexplorer = "IE10"; } else { return myexplorer = "IE11"; } } else { return myexplorer = "IE9"; } } } else {//非IE if (explorer.indexOf("LBBROWSER") >= 0) { return myexplorer = "猎豹"; } else if (explorer.indexOf("360ee") >= 0) { return myexplorer = "360极速浏览器"; } else if (explorer.indexOf("360se") >= 0) { return myexplorer = "360安全浏览器"; } else if (explorer.indexOf("se") >= 0) { return myexplorer = "搜狗浏览器"; } else if (explorer.indexOf("aoyou") >= 0) { return myexplorer = "遨游浏览器"; } else if (explorer.indexOf("qqbrowser") >= 0) { return myexplorer = "QQ浏览器"; } else if (explorer.indexOf("baidu") >= 0) { return myexplorer = "百度浏览器"; } else if (explorer.indexOf("Firefox") >= 0) { return myexplorer = "火狐"; } else if (explorer.indexOf("Maxthon") >= 0) { return myexplorer = "遨游"; } else if (explorer.indexOf("Chrome") >= 0) { return myexplorer = "谷歌内核浏览器";//谷歌(或360伪装) } else if (explorer.indexOf("Opera") >= 0) { return myexplorer = "欧朋"; } else if (explorer.indexOf("TheWorld") >= 0) { return myexplorer = "世界之窗"; } else if (explorer.indexOf("Safari") >= 0) { return myexplorer = "苹果"; } else { return myexplorer = "其他"; } } }
4、获取当前时间戳
/** * 获取当前时间戳 */ function getTimestamp() { var timestamp = Date.parse(new Date()); return timestamp; }
5、获取当前访问的域名
获取当前页面的域名,并对以 www. 开头的域名进行处理,去掉开头的 www. 部分。
function currentDomain() { var url = window.location.host; if (url.startsWith('www.')) { url = url.substring(4); } return url; }