登录  /  注册
首页 > web前端 > js教程 > 正文

JS 精确统计网站访问量的实例代码_javascript技巧

php中文网
发布: 2016-05-16 17:29:52
原创
896人浏览过
复制代码 代码如下:

/**
 * vlstat 浏览器统计脚本
 */
var statIdName = "vlstatId";
var xmlHttp;
/**
 * 设置cookieId
 */
function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()) + ";path=/;domain=cecb2b.com";
}
/**
 * 获取cookieId
 */
function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) {
                c_end = document.cookie.length;
            }
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}
/**
 * 获取当前时间戳
 */
function getTimestamp() {
    var timestamp = Date.parse(new Date());
    return timestamp;
}
/**
 * 生成statId
 */
function genStatId() {
    var cookieId = getTimestamp();
    cookieId = "vlstat" + "-" + cookieId + "-" + Math.round(Math.random() * 3000000000);
    return cookieId;
}
/**
 * 设置StatId
 */
function setStatId() {
    var cookieId = genStatId();
    setCookie(statIdName, cookieId, 365);
}
/**
 * 获取StatId
 */
function getStatId() {
    var statId = getCookie(statIdName);
    if (statId != null && statId.length > 0) {
        return statId;
    } else {
        setStatId();
        return getStatId();
    }
}
/**
 * 获取UA
 */
function getUA() {
    var ua = navigator.userAgent;
    if (ua.length > 250) {
        ua = ua.substring(0, 250);
    }
    return ua;
}
/**
 * 获取浏览器类型
 */
function getBrower() {
    var ua = getUA();
    if (ua.indexOf("Maxthon") != -1) {
        return "Maxthon";
    } else if (ua.indexOf("MSIE") != -1) {
        return "MSIE";
    } else if (ua.indexOf("Firefox") != -1) {
        return "Firefox";
    } else if (ua.indexOf("Chrome") != -1) {
        return "Chrome";
    } else if (ua.indexOf("Opera") != -1) {
        return "Opera";
    } else if (ua.indexOf("Safari") != -1) {
        return "Safari";
    } else {
        return "ot";
    }
}
/**
 * 获取浏览器语言
 */
function getBrowerLanguage() {
    var lang = navigator.browserLanguage;
    return lang != null && lang.length > 0 ? lang : "";
}
/**
 * 获取操作系统
 */
function getPlatform() {
    return navigator.platform;
}
/**
 * 获取页面title
 */
function getPageTitle() {
    return document.title;
}
/**
 * 创建一个form
 *
 * @return
 */
function createSubmitForm() {
    var frm = document.createElement("form");
    document.body.appendChild(frm);
    frm.method = "POST";
    return frm;
}
/**
 * 为form创建一个元素
 *
 * @param inputForm
 * @param elementName
 * @param elementValue
 * @return
 */
function createFormElement(frmInput, elementName, elementValue) {
    var element = document.createElement("input");
    element.setAttribute("id", elementName);
    element.setAttribute("name", elementName);
    element.setAttribute("type", "hidden");
    element.setAttribute("value", elementValue);
    frmInput.appendChild(element);
    return element;
}
/**
 * 构造XMLHttpRequest对象
 *
 * @return
 */
function createXMLHttpRequest() { 
 if (window.ActiveXObject) { 
  xmlHttp = new ActiveXObject('Microsoft.XMLHTTP'); 
 } else if (window.XMLHttpRequest) { 
  xmlHttp = new XMLHttpRequest(); 
 } 
}
/**
 * url指定跳转页,data是要post的数据。func类似于函数指针
 *
 * @param url
 * @param data
 * @param func
 * @return
 */
function AjaxPost(url, data, func) {
    var httpRequest = createHttpRequest();
    if (httpRequest) {
        httpRequest.open("POST", url, true);
        httpRequest.setRequestHeader("content-length", data.length);
        httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        httpRequest.send(data);
        httpRequest.onreadystatechange = function() {
            if (httpRequest.readyState == 4) {
                try {
                    if (httpRequest.status == 200) {
                        if (func) {
                            // 这里可以调用想要的函数
                            func(httpRequest.responseText);
                        }
                    }
                } catch (e) {
                    alert("Error XMLHttpRequest!");
                }
            }
        }
    } else {
        alert("Error initializing XMLHttpRequest!");
    }
}
function vlstatInitLE(vlch, vlch1, vlch2, vlch3) {
    var p;
    var vlstatCH = vlch != null && vlch.length > 0 ? vlch : "";
    var vlstatCH1 = vlch1 != null && vlch1.length > 0 ? vlch1 : "";
    var vlstatCH2 = vlch2 != null && vlch2.length > 0 ? vlch2 : "";
    var vlstatCH3 = vlch3 != null && vlch3.length > 0 ? vlch3 : "";
    var vlstatCookieId = getStatId();
    var vlstatUA = encodeURIComponent(getUA());
    var vlstatIPAddress = document.localName;
    var vlstatREFURL = encodeURIComponent(document.referrer);
    var vlstatURL = encodeURIComponent(document.URL);
    var vlstatScreenX = screen.width;
    var vlstatScreenY = screen.height;
    var vlstatOS = getPlatform();
    var vlstatBrower = getBrower();
    var vlstatBrowerLanguage = getBrowerLanguage();
    var vlstatPageTitle = encodeURIComponent(getPageTitle());
    var vlstatAction = "index.php";
    p = "cookieId=" + vlstatCookieId + "&ua=" + vlstatUA + "&ip=" + vlstatIPAddress + "&refurl="
            + vlstatREFURL + "&url=" + vlstatURL + "&screenX=" + vlstatScreenX + "&screenY=" + vlstatScreenY
            + "&os=" + vlstatOS + "&brower=" + vlstatBrower + "&browerLang=" + vlstatBrowerLanguage
            + "&title=" + vlstatPageTitle + "&ch=" + vlstatCH + "&ch1=" + vlstatCH1 + "&ch2=" + vlstatCH2
            + "&ch3=" + vlstatCH3;
    var urlGo = vlstatAction + "?" + p;
    createXMLHttpRequest();
 xmlHttp.open('GET', urlGo);
 xmlHttp.send(null);
}
智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号