开发者

javascript cookie problem

开发者 https://www.devze.com 2023-01-09 08:58 出处:网络
I am very new to javascript. Today I tried this function which I found on the Internet: function get_cookie ( cookie_name )

I am very new to javascript.

Today I tried this function which I found on the Internet:

function get_cookie ( cookie_name )
{
    var cookie_string = document.cookie ;
    if (cookie_string.length != 0) {
        var cookie_value = cookie_string.match (
                        '(^|;)[\s]*' +
                        cookie_name +
                        '=([^;]*)' );
        return decodeURIComponent ( cookie_value[2] ) ;
    }
    return '' ;
}

If I call this function once, it works; however if i call this function twice, it's not working.

function onLoadThis() {
    var t = get_cookie('t'); // if i add another one, not working
    var s = get_cookie('s');
    // more code
}

Both cookies exist. Is there any workaround to make it work, or can two variable merged in a cookie? Thank you for your 开发者_如何学Pythonhelp.


You could try instead a different regexp:

Not

var cookie_value = cookie_string.match (
                        '(^|;)[\s]*' +
                        cookie_name +
                        '=([^;]*)' );

But

var cookie_value = cookie_string.match (
                       '([^;]*)[\s]*' +
                       cookie_name +
                       '=([^;]*)' );
0

精彩评论

暂无评论...
验证码 换一张
取 消