UI Drafter

How to show the 'allow cookie' prompt?

by Eric Fortis

Scenario

  • Your web app needs the IndexedDB, or cookies (e.g. for login)
  • Your visitor has cookies disabled

By the way, having cookies allowed implies that the IndexedDB is allowed as well.

In these scenarios, it is common to see web applications crashing out. Or, that they don’t show the button to allow cookies in the address bar, which makes it easy to authorize them.

Allow cookie prompt

Solution

if (!navigator.cookieEnabled) {
  alert('Please allow cookies to continue')
  try { document.cookie = 'A=1' }
  catch {}
}

The alert is optional. The important part is trying to set a cookie, for example A=1, and making sure it’s catching the thrown exception of not having cookies allowed, so the web application doesn’t crash.

Sponsored by: