简体   繁体   中英

Is there a way I can have Javascript tell me why a cookie wasn't set?

Here is my code for setting a cookie:

function setCookie(c_name, value, expiredays)
{
    alert("setting cookie!");
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) +
    ((expiredays === null) ? "" : ";expires=" + exdate.toUTCString());
}

I'm using Phonegap to make an app on Android phones, which should just use and set cookies like a regular browser. It doesn't seem to be setting the cookie, though, and because of the environment (on my pnone), I can't debug.

So, is there a way I can make an alert to tell me if document.cookie fails, and ideally any error message that might tell me why?

I tried just doing alert(document.cookie) , but it's not outputting anything, so I'm hoping there's another way to trap the output.

You can use try/catch statement:

function setCookie(c_name, value, expiredays)
{
    try {
       alert("setting cookie!");
       var exdate = new Date();
       exdate.setDate(exdate.getDate() + expiredays);
       document.cookie = c_name + "=" + escape(value) +
       ((expiredays === null) ? "" : ";expires=" + exdate.toUTCString());
    } catch(error) {
      // deal with errors
      alert(error);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM