How to store other languages (unicode) in cookies and get it back again

前端 未结 1 939
时光取名叫无心
时光取名叫无心 2020-12-21 05:01

Can anyone help me understand how to store a cookie value that is in another language and than how to retrieve it again in that language.

I seem to have my foreign

相关标签:
1条回答
  • 2020-12-21 05:19

    Use encodeURIComponent() when setting the cookie and decodeURIComponent() when retrieving it.

    var cookieValue = document.getElementsByTagName('input')[0].value;
    document.cookie = "lboxcook=" + encodeURIComponent(cookieValue);
    
    function get_cookie(cookie_name) {
        var results = document.cookie.match ('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');
        return results ? decodeURIComponent(results[2]) : null;
    }
    
    0 讨论(0)
提交回复
热议问题