开发者

Javascript to delete cookie on android web browser

开发者 https://www.devze.com 2023-01-29 19:16 出处:网络
What is the javascript to delete a cookie on android web browser. The usual meth开发者_JS百科od of setting an expiration date of the cookie to a date in the past does not work in android web browser.

What is the javascript to delete a cookie on android web browser. The usual meth开发者_JS百科od of setting an expiration date of the cookie to a date in the past does not work in android web browser.

For e.g. the below code works in desktop web browsers and mobile safari but does not work in android web browser.

document.cookie = 'cookiename=cookievalue; expires=Thu, 2 Aug 2001 20:47:11 UTC; path=/';


I've seen browsers (actually in TV sets, but not sure which it was exactly), that did not accept the 'expires=' field (with an absolute date), but worked well with 'max-age=' (live-time in number of seconds from now). So maybe try to delete the cookie by:

document.cookie = 'cookiename=; max-age=0; path=/';


Have you tried also including the domain in the cookie setting line? I remember that in some cases you had to be very explicit with the domain and path matching (or being compatible with) the current document location in order to delete a cookie:

document.cookie='cookiename=cookievalue; path=/; domain=current-domain; expires=Thu, 01 Jan 1970 00:00:01 GMT';


that works for me

document.cookie = yourCookieName + '=' + // NO value here 
    ';expires=Thu, 01-Jan-1970 00:00:01 GMT';

if used path and/or domain at set time, add em

document.cookie = yourCookieName + '=' + // NO value here
    ';path=' + yourCookiePath +
    ';domain=' + yourCookieDomain +
    ';expires=Thu, 01-Jan-1970 00:00:01 GMT';


While this is a roundabout way of doing so, create and instantiate a JavascriptInterface that will be called from the webpage Javascript. Set that Javascript interface to your Webview using

webSettings.setJavascriptEnabled(true);

and

webView.addJavascriptInterface(new JavaScriptInterfaceImplementation(), INTERFACE_NAME);

On your web page you then invoke

javascript:INTERFACE_NAME.yourSessionClearingMethod();

which should contain

CookieManager cookieManager = CookieManager.getInstance();        
cookieManager.removeAllCookie(); // or cookieManager.removeSessionCookie();

to clear said unwanted cookie(s).

0

精彩评论

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