Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to check the value of Firefox DNT with javascript?

I'm working on a javascript advertising engine, and I would like for it to respect Firefox DNT header.

Is there any way javascript can check if the user has set DNT to on or off in firefox (or has set no preferences) without getting help from the server?

like image 250
Arsen Zahray Avatar asked Jun 05 '13 18:06

Arsen Zahray


People also ask

What is http DNT?

The DNT (Do Not Track) request header indicates the user's tracking preference. It lets users indicate whether they would prefer privacy rather than personalized content. Header type.


1 Answers

I think you're looking for navigator.doNotTrack:

console.log(window.navigator.doNotTrack); 
// prints "yes" if DNT is enabled; otherwise this is "unspecified" in Firefox

MDN explains that in Firefox:

When the do-not-track header sends "1", navigator.doNotTrack is "yes". When the header is unset, navigator.doNotTrack is "unspecified". When the header sends "0" (currently unsupported in Firefox), navigator.doNotTrack is "no".

In other browsers:

IE9, Opera 12, and Safari 5.1 are based on an earlier version of this specification where navigator.doNotTrack is the value sent for the do-not-track header.

IE9 uses a vendor prefix, i.e., navigator.msDoNotTrack

So, you might detect DNT in general by doing:

var isDNT = navigator.doNotTrack == "yes" || navigator.doNotTrack == "1" || 
              navigator.msDoNotTrack == "1";
like image 144
apsillers Avatar answered Sep 20 '22 17:09

apsillers