4

One of my jquery plugins is having issues, and the issue occurs when private browsing is turned on in ios.

Is there a way to check this?

user759235
  • 2,147
  • 3
  • 39
  • 77
  • BTW: My opinion is a web app shouldn't be able to tell a difference, and it's a browser bug if it can. (This obviously doesn't solve your problem though; I wonder what's the reason.) – Kos Oct 10 '12 at 14:58

2 Answers2

12

In private mode user can't use local storage try this:

var storageTestKey = 'sTest',
    storage = window.sessionStorage;

try {
  storage.setItem(storageTestKey, 'test');
  storage.removeItem(storageTestKey);
} catch (e) {
  if (e.code === DOMException.QUOTA_EXCEEDED_ERR && storage.length === 0) {
    // private mode
  } else {
    throw e;
  }
}
adriaan
  • 1,088
  • 1
  • 12
  • 29
anton_byrna
  • 2,477
  • 1
  • 18
  • 31
  • Can't tell on iOS, but doesn't work in Chrome. Here's a fiddle: http://jsfiddle.net/n49yQ/ – Kos Oct 10 '12 at 14:56
  • 2
    That's because Safari and Chrome handle the idea of storage during a private session differently. Chrome will still allow **temporary** session storage when in 'incognito' mode, whereas Safari disable access completely. I prefer Chrome's implementation because HTML5 apps will still be able to cache their files/images. – stevokk Jan 21 '13 at 14:59
  • 1
    @Kos Your jsfiddle does not work anymore. Here is a working fiddle: http://jsfiddle.net/harianus/aueb9u74/ It alerts when Safari Prived mode is detected. – adriaan Jul 22 '15 at 08:37
5

I've found an Answear at GitHub and tested it: Working on IOS 11!

var isPrivate = false;
try {
  window.openDatabase(null, null, null, null);
} catch (_) {
  isPrivate = true;
}
alert((isPrivate ? 'You\'re' : 'You aren\'t')  + ' in private browsing mode');
Daniel Fisher lennybacon
  • 3,865
  • 1
  • 30
  • 38
  • Does not appear to work anymore in IOS 13.0, but oddly enough, in IOS 13.0.1 it's fine again. – Eon Oct 10 '19 at 13:15