JS分析url中的参数

function GetRequest() { 
    var url = window.location.search; //获取url中"?"符后的字串 
        var theRequest = new Object(); 
        if (url.indexOf("?") != -1) { 
        var str = url.substr(1); 
        strs = str.split("&"); 
        for(var i = 0; i < strs.length; i ++) { 
            theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]); 
        } 
    } 
    return theRequest; 
} 

来源:https://www.jb51.net/article/120452.htm

把分析数据存入到cookie中

var url = window.location.search;
if (url.indexOf("?") != -1) {
    var str = url.substr(1);
    strs = str.split("&");
    for(var i = 0; i < strs.length; i ++) {
        var cookie_name = encodeURI(strs[i].split("=")[0]),
            cookie_value = encodeURI(strs[i].split("=")[1]);
        setCookie(cookie_name, cookie_value);
    }
}

function setCookie(name, value){
    var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ value + ";expires=" + exp.toGMTString();
}

390 total views, 2 views today

Revisions

There are no revisions for this post.

No comments yet.

发表评论