admin管理员组

文章数量:1296291

My jquerymobile App requires the use of localStorage and sessionstorage e.t.c, I've been giving a prompt to users without cookie support and telling them to enable cookies, but if a user has private browsing enable, this create cookie test I'm doing doesn't work and they just get a still erroneous screen, does anyone know how I could test if the user has private browsing enabled?

Thanks

My jquerymobile App requires the use of localStorage and sessionstorage e.t.c, I've been giving a prompt to users without cookie support and telling them to enable cookies, but if a user has private browsing enable, this create cookie test I'm doing doesn't work and they just get a still erroneous screen, does anyone know how I could test if the user has private browsing enabled?

Thanks

Share Improve this question asked Mar 11, 2012 at 21:34 williamsandonzwilliamsandonz 16.5k23 gold badges106 silver badges189 bronze badges 4
  • if i try create a localstorage entry when user is in this mode, the app bugs, even when in try catch – williamsandonz Commented Mar 11, 2012 at 23:00
  • What is the specific error it throws? – levi Commented Mar 11, 2012 at 23:37
  • its not throwing an error, it just fails silently! – williamsandonz Commented Mar 11, 2012 at 23:43
  • oh wait got it, QUOTA_EXCEEDED_ERR:DOM Exception 22: An attempt was made to add something to storage – williamsandonz Commented Mar 11, 2012 at 23:58
Add a ment  | 

1 Answer 1

Reset to default 9

I don't have an Iphone to test this on, but in the desktop Safari browser (in private mode) running the below function does catch the error and handles it as expected.

function storageEnabled() {
    try {
        localStorage.setItem("__test", "data");
    } catch (e) {
        if (/QUOTA_?EXCEEDED/i.test(e.name)) {
            return false;
        }
    }
    return true;
}

if (!storageEnabled()) alert('localStorage not enabled');

Jsfiddle: http://jsfiddle/B9eZ5/

本文标签: javascriptHow to detect users on an iPhone with quotPrivate Browsingquot enabledStack Overflow