Skip to main content

How to correctly check for Do Not Track with JavaScript

Every modern browser has a Do Not Track setting, which tells sites whether the user wants to be tracked or not. Many sites don't respect this setting, but it's there regardless.

However, checking the value of this setting is far more complicated than it needs to be. Major browsers are split on whether it should be `navigator.doNotTrack` or `window.doNotTrack`, and the value can either be a string, a Boolean, or `undefined`. Here's how it boils down:

  1. Chrome, Opera, and other Blink-based browsers use `navigator.doNotTrack`, with values of either `1` for true or `0` for false.
  2.  Firefox also uses `navigator.doNotTrack` with the same values, but older versions use a different syntax. Prior to v32, Firefox would report `yes`, `no`, or `unspecified` (more info).
  3. Safari 7.1.3 and later use `window.doNotTrack`, while older versions use `navigator.doNotTrack`.
  4. Microsoft Edge and Internet Explorer 11 use `window.doNotTrack`, and reports `1` when enabled and 'unspecified' (Edge) or 'null' (IE11) when disabled.
  5. Internet Explorer 10 and older use `navigator.msDoNotTrack`, which return '1' or '0'.
  6. On all versions of Internet Explorer, you can also check `window.external.msTrackingProtectionEnabled()`, which returns either `true` or `false`. This was removed in MS Edge.

Suffice to say, the implementation of DND across browsers is pretty inconsistent. But with all those different methods in mind, it's not too difficult to create a fool-proof and universal DNT check:

if (window.doNotTrack || navigator.doNotTrack || navigator.msDoNotTrack || 'msTrackingProtectionEnabled' in window.external) {

    // The browser supports Do Not Track!

    if (window.doNotTrack == "1" || navigator.doNotTrack == "yes" || navigator.doNotTrack == "1" || navigator.msDoNotTrack == "1" || window.external.msTrackingProtectionEnabled()) {

        // Do Not Track is enabled!

    } else {

        // Do Not Track is disabled!

    }

} else {
    
    // Do Not Track is not supported

}

The above code works on every major browser, including Internet Explorer all the way back to IE6 (and possibly earlier, I can't tell when the `msTrackingProtectionEnabled` function was implemented). You can try a demo here.

Hopefully this helps anyone else running into this problem!

This post is also available on DEV.to.

Popular posts from this blog

Goodbye, @ItMeIRL.

All the way back in October 2015 (that seems like ages ago!), I made a Twitter bot called @ItMeIRL. As the name implies, it posts content from the /r/me_irl subreddit. Over time, it grew a pretty sizable audience—the account now sits at nearly 400k followers.

For some of that time, I made it as something mostly for myself and friends to enjoy. But as time went on, it actually became a fun programming side-project. I wrote my own automated bot (Tootbot) mostly from scratch, and I became more experienced with the Python programming language in the process. There are now over a dozen Twitter and Mastodon accounts using Tootbot, and I've loved seeing what others create with it.

But over the past eight months or so, my desire to keep running the account has essentially vanished. Back in January, someone with thousands of followers (who shall remain nameless) became upset that my bot posted screenshots of his tweets. When I didn't delete them in a timely fashion, he told his followe…

Hacking Android Wear: Part One

A few weeks ago, I finally got my hands on the Samsung Gear Live. It's a smartwatch powered by Google's new Android Wear operating system. And like pretty much everything I buy with a screen, I started messing around with it.

It turns out that Android Wear isn't as far off from normal Android as I thought - with some caveats, it's possible to install plain Android apps manually. So I published a video of Minecraft Pocket Edition on my watch, which became somewhat popular. I've made a few more videos since then, my most popular one (currently) being a demo of Windows 95.

Many people have asked me for a tutorial, so I'm splitting this into a few sections for better organization. This part introduces you to ADB and manually installing applications. This will not void your warranty on your watch, and this works with any Android Wear watch. This may seem long, but that's just because I've explained everything in great detail for beginner users.
Installing ADBAD…

How to make a flash drive sync with OneDrive

I’ve wanted cloud storage services like OneDrive or Dropbox to make flash drives that sync with their services for a while, and there doesn’t seem to be anything like it on the market. This would be immensely useful on shared/public computers, for when you need to access files without using the often less-useful web app or installing the normal sync app.

My goal was to make a flash drive that fully synced with OneDrive, that could be used on public (specifically school computers). This should work for any Windows XP SP2+ computer, as long as whatever restrictions are in place allow running programs from a flash drive (some organizations only allow pre-installed software to be run, for instance). So here’s how I did it.
Setting it up First, create folders on your flash drive called ‘OneDrive’ and ‘Sync’. The first will serve as the folder where your OneDrive files are kept, and the second where the program that syncs your files will be installed to. It should look something like this: