javascript cookie problem
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 +
'=([^;]*)' );
精彩评论