13

On the new iOS 11 Safari and MacOS High Sierra Safari, that trick of seeing if window.localStorage.setItem('test', 1); (see https://stackoverflow.com/a/17741714/1330341) throws an error no longer works, because it no longer throws an error, and it also properly sets the localStorage item. Has anyone figured out any other way to check for private browsing mode in the new versions of Safari?

JYeh
  • 4,273
  • 3
  • 27
  • 40

4 Answers4

1

Haven't actually tried it, but from reading Apple's document:

https://support.apple.com/kb/ph21413?locale=en_US

It lists various characteristics of private mode browsing (~snip):

When you use a Private Browsing window:

  • Each tab in the window is isolated from the others, so websites you view in one tab can’t track your browsing in other tabs.

  • Safari doesn’t remember the webpages you visit or your AutoFill information.

  • Safari doesn’t store your open webpages in iCloud, so they aren’t shown when you view all your open tabs from other devices.

  • Your recent searches aren’t included in the results list when you use the Smart Search field.

  • Items you download aren’t included in the downloads list. (The items do remain on your computer.)

  • If you use Handoff, Private Browsing windows are not passed to your iOS devices or other Mac computers.

  • Safari doesn’t remember changes to your cookies or other website data. Safari also asks websites and others who provide those sites with content (including advertisers) not to keep track of your browsing, although it is up to the websites to honor this request.

  • Plug-ins that support Private Browsing stop storing cookies and other tracking information.

From the above, in particular I found interesting that Safari specifically asks websites to "not track" the browsing. This could potentially be a mechanism to look for, to determine if using private browsing.

See this answer for an example:

Implementing Do not track in asp.net mvc

Again, haven't tested and unsure if it will work, but if not the list provides other potential options. HTH.

kvr
  • 573
  • 2
  • 8
1

I find a solution here:

https://gist.github.com/cou929/7973956#gistcomment-2272103

var isPrivate = false;
try {
   window.openDatabase(null, null, null, null);
} catch (_) {
   isPrivate = true;
}
alert((isPrivate ? 'You\'re' : 'You aren\'t')  + ' in private browsing mode');

Hope it helps :)

GOR BAND
  • 11
  • 1
0

Quote from apple's website. https://support.apple.com/kb/ph21413?locale=en_US

Websites can’t modify information stored on your device, so services normally available at such sites may work differently until you turn off Private Browsing

So, store a test variable, change its value, then read the test variable. If you get an exception, are unable to find the variable, the value didn't change or you get a null/undefined value back, they are most likely in private mode.

Alternatively, in private browsing, you have no stored search history accessible. So, redirect to a new page in your site on startup and then test if you have any previous history. If not and the fact that you are getting a Do Not Track most likely means your in private mode on safari.

Please note that I have not tested this. This is based off the information provided by Apple in the above link.

thatguy
  • 1,249
  • 9
  • 26
-1

Thing that I realized is Safari throws a "Quota Exceeded" error in the Private mode. So here is what I did!

isPrivateMode: function () {
  if (localStorage.length === 0) {
    try {
      localStorage.setItem('isLocalStorageAvaialble', 'That is being tested!');
      localStorage.removeItem('isLocalStorageAvaialble');
      return false;
    } catch (e) {
      return true;
    }
  }
}

Checking the length of localStorage is important for the fact that, if you are trying this method on a browser that supports localStorage, but is full, you still will get the "Quota Exceeded" error. In private mode, the length of localStorage is always 0.

Hope this helps!

Shreerang
  • 367
  • 4
  • 15