Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect users on an iPhone with "Private Browsing" enabled?

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

like image 647
williamsandonz Avatar asked Mar 11 '12 21:03

williamsandonz


1 Answers

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.net/B9eZ5/

like image 187
levi Avatar answered Nov 18 '22 03:11

levi