Javascript : How to check a browser's Do Not Track status?
Problem :
You work for an advertisement network and you want to respect a visitor's Do Not Track status. How to detect or check if a visitor's browser Do Not Track is turn on or off?
Solution :
Use Javascript to extract the doNotTrack variable from the browser.
- Chrome and Firefox based browser use
navigator.doNotTrack
- Safari is
window.doNotTrack
and - Microsoft Internet Explorer use
navigator.msDoNotTrack
The value returned is usually yes
or 1
.
For example, to check Do Not Track status for most common browsers
var DNT = window.doNotTrack == "1" || navigator.doNotTrack == "yes" || navigator.doNotTrack == "1" || navigator.msDoNotTrack == "1";
or
console.log(navigator.doNotTrack);
IF DNT is true, then the browser's Do Not Track is turned ON. If false or unspecified, then OFF.
[This solution will only work properly if Javascript is turned on.]
NOTE :
At the moment of writing, most ads networks will not honor Do Not Track status. Browser based add-on like Privacy Badger will be able to tell you which trackers will honor or ignore the Do Not Track status. In the long run, it will be wise to respect a visitor's decision to turn on Do Not Track.
Special thanks to Mr. Джон Сора of Russia for introducing Privacy Badger to me.
References :
https://developer.mozilla.org/en-US/docs/Web/API/navigator/doNotTrack
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.6k Unix/Linux : How to find out the hard disk size?
+5.3k Golang : Generate Interleaved 2 inch by 5 inch barcode
+9.8k Golang : Load ASN1 encoded DSA public key PEM file example
+27.4k Golang : Convert CSV data to JSON format and save to file
+12.3k Golang : How to display image file or expose CSS, JS files from localhost?
+27.5k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+15.5k Golang : Find location by IP address and display with Google Map
+16.3k Golang : Loop each day of the current month example
+12.1k Golang : Sort and reverse sort a slice of runes
+11k Golang : Create Temporary File
+5.8k Golang : Fix opencv.LoadHaarClassifierCascade The node does not represent a user object error