All Products
Search
Document Center

SuperApp:Cookie

Last Updated:Oct 25, 2024

This topic describes the JavaScript APIs of WVCookie. You can refer to this topic when you create HTML5 apps or miniapps. You can use the JavaScript APIs of WVCookie to read and write cookies.

Important

The API operations may fail. Exercise caution when you call these operations.

WVCookie.read

Reads all cookies from the URL that you specify. If you want to read a cookie from the domain that sets it, we recommend that you use JavaScript. If you want to allow a domain to read a cookie set by a different domain, we recommend that you call this API operation.

Input parameters

  • [string]url: the URL from which you want to read cookies.

Callback parameters

Callback parameters are passed by using the callback method. If cookies are obtained from the specified URL, the success callback is invoked. Otherwise, the failure callback is invoked.

  • [object]value: the cookies that are obtained from the specified URL. All cookies are stored as key-value pairs.

var params = {
        // The URL from which you want to read cookies.
        url: 'http:// 
xxx 
.com'
};
window.WindVane.call('WVCookie', 'read', params, function(e) {
        alert('success: ' + JSON.stringify(e));
}, function(e) {
        alert('failure: ' + JSON.stringify(e));
});

WVCookie.write

Writes a cookie. If you want to write a cookie in a domain, we recommend that you use JavaScript. If you want to write a cookie that is set in a domain to a different domain, we recommend that you call this API operation.

Input parameters

  • [string]: the key of the cookie that you want to write. The following sample code is for reference only.

  • [string]domain: the domain name to which you want to write the cookie.

  • [string]max-age: (optional) the validity period of the cookie.

  • [string]path: (optional) the path to which you want to write the cookie. Default value: '/'

Note

You can also add other cookie attributes in the same way as you add max-age and path. You can only set one cookie each time you call this API operation.

Callback parameters

No callback parameters exist. If the cookie is written successfully, the success callback is invoked. Otherwise, the failure callback is invoked.

// The cookie that you want to write. The cookie name is cookieKey and the value is cookieValue in the following sample code. 
var params = {
        cookieKey: 'cookieValue',
        domain: 'h5.m.taobao.com',
};
window.WindVane.call('WVCookie', 'write', params, function(e) {
        alert('success');
}, function(e) {
        alert('failure: ' + JSON.stringify(e));
});