[BOM]获取url参数并赋值给cookie

不安全,挺傻的,可以偷懒。

 

    <script>
        function init_params() {
            var url = 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;
        }
        function set_cookie(key, value, day, domain) {
            var exdatestr = ''
            domain = domain || location.hostname
            if (day) {
                var date = new Date()
                date.setDate(date.getDate() + day)
                exdatestr = ';expires=' + date.toGMTString()
            }
            var cookie = key + '=' + value + exdatestr + ';path=/;domain=' + domain
            document.cookie = cookie
            console.log('document.cookie', cookie);
        }

        var params = init_params()
        console.log('params', params)
        if (params && params.session_id){
            set_cookie('cb-session-id', params.session_id)
        }
    </script>

 

posted @ 2024-02-09 02:32  夕苜19  阅读(10)  评论(0编辑  收藏  举报