Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Detect if the browser is running in Private mode
/**
* Detect if the browser is running in Private mode
*
* @returns {Promise}
*/
function isPrivateMode() {
return new Promise((resolve) => {
const on = () => resolve(true); // is in private mode
const off = () => resolve(false); // not private mode
const testLocalStorage = () => {
try {
if (localStorage.length) off();
else {
localStorage.x = 1;
localStorage.removeItem('x');
off();
}
} catch (e) {
// Safari only enables cookie in private mode
// if cookie is disabled, then all client side storage is disabled
// if all client side storage is disabled, then there is no point
// in using private mode
navigator.cookieEnabled ? on() : off();
}
};
// Chrome & Opera
if (window.webkitRequestFileSystem) {
return void window.webkitRequestFileSystem(0, 0, off, on);
}
// Firefox
if ('MozAppearance' in document.documentElement.style) {
if (indexedDB === null) return on();
const db = indexedDB.open('test');
db.onerror = on;
db.onsuccess = off;
return void 0;
}
// Safari
const isSafari = navigator.userAgent.match(/Version\/([0-9\._]+).*Safari/);
if (isSafari) {
const version = parseInt(isSafari[1], 10);
if (version < 11) return testLocalStorage();
try {
window.openDatabase(null, null, null, null);
return off();
} catch (_) {
return on();
};
}
// IE10+ & Edge InPrivate
if (!window.indexedDB && (window.PointerEvent || window.MSPointerEvent)) {
return on();
}
// default navigation mode
return off();
});
}
isPrivateMode().then((inPrivate) => {
console.log('is in private mode: ', inPrivate);
});
@jherax

This comment has been minimized.

Copy link
Owner Author

jherax commented Dec 1, 2016

This script can be found in the following stackoverflow: Detecting if a browser is using Private Browsing mode

@jmboiton

This comment has been minimized.

Copy link

jmboiton commented Jun 29, 2017

I would like to highlight this comment from the same stackoverflow question :

Promises aren't natively supported in IE10 or 11. I'd assume you used a polyfill here

@lukasotocerny

This comment has been minimized.

Copy link

lukasotocerny commented Dec 27, 2017

It fails to detect Private mode in Safari Version 11.0.2

@subversivo58

This comment has been minimized.

Copy link

subversivo58 commented Jan 24, 2018

Suggest: fix Chrome and Opera handler... using RequestFileSystem third param is targed to success and last for error, like:

return void window.webkitRequestFileSystem(0, 0, on, off);
@Maykonn

This comment has been minimized.

Copy link

Maykonn commented Jul 27, 2018

@gaurav150493

This comment has been minimized.

Copy link

gaurav150493 commented Sep 24, 2018

It fails to detect Private mode in Safari Version 11.0.2

try {
  window.openDatabase(null, null, null, null);
} catch (_) {
  isPrivate = true;
};

Use this code for iOS > 11

@Elastic1

This comment has been minimized.

Copy link

Elastic1 commented Nov 20, 2018

for safari

    const isSafari = navigator.userAgent.match(/Version\/([0-9\._]+).*Safari/);
    if (isSafari) {
      const version = parseInt(isSafari[1], 10);
      if (version >= 11) {
         try {
           window.openDatabase(null, null, null, null);
           return off();
         } catch (_) {
           return on();
         };
      } else if (version < 11) {
        return testLocalStorage();
      }
   }
@jherax

This comment has been minimized.

Copy link
Owner Author

jherax commented Feb 6, 2019

for safari

const isSafari = ...

Thanks for the improvement @Elastic1

Added to the snippet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.