<!DOCTYPE html>
<html>
  <head>
    <meta charset=utf-8 />
    <title>isPrivateMode</title>
    <script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.min.js"></script>
  </head>
  <body>
    <h4>Testing</h4>
    <p>
      <code>isPrivateMode(): Promise</code>
    </p>
    
    <hr />
    
    <p>
      An <code>alert()</code> will notify if you are browsing in private mode.
    </p>
    
    <hr />
    
    <aside>
      <code>Promise</code> object is polyfilled.
    </aside>
  </body>
</html>
 
aside {
  font-style: italic;
}
 
function isPrivateMode() {
  "use strict";
  
  return new Promise(function (resolve) {
    // is in private mode
    var yes = function yes() {
      return resolve(true);
    };
    // not in private mode
    var not = function not() {
      return resolve(false);
    };
    var testLocalStorage = function testLocalStorage() {
      try {
        if (localStorage.length) not();
        else {
          localStorage.x = 1;
          localStorage.removeItem('x');
          not();
        }
      } 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 ? yes() : not();
      }
    };
    
    // Chrome & Opera
    var fs = window.webkitRequestFileSystem || window.RequestFileSystem;
    if (fs) {
      return void fs(window.TEMPORARY, 100, not, yes);
    }
    
    // Firefox
    if ('MozAppearance' in document.documentElement.style) {
      if (indexedDB === null) return yes();
      var db = indexedDB.open('test');
      db.onerror = yes;
      db.onsuccess = not;
      return void 0;
    }
    
    // Safari
    var isSafari = navigator.userAgent.match(/Version\/([0-9\._]+).*Safari/);
    if (isSafari) {
      var version = parseInt(isSafari[1], 10);
      if (version < 11) return testLocalStorage();
      try {
        window.openDatabase(null, null, null, null);
        return not();
      } catch (_) {
        return yes();
      }
    }
    
    // IE10+ & Edge InPrivate
    if (!window.indexedDB && (window.PointerEvent || window.MSPointerEvent)) {
      return yes();
    }
    
    // default navigation mode
    return not();
  });
}
isPrivateMode().then(function (isPrivate) {
  alert('Browsing in private mode? ' + isPrivate);
});
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers