最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin9浏览0评论

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?

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?

Share Improve this question edited Jun 5, 2013 at 18:45 BoltClock 725k165 gold badges1.4k silver badges1.4k bronze badges asked Jun 5, 2013 at 18:40 Arsen ZahrayArsen Zahray 25.4k51 gold badges141 silver badges234 bronze badges 1
  • 1 Note that Firefox is not the only browser that implements DNT. – BoltClock Commented Jun 5, 2013 at 18:45
Add a ment  | 

1 Answer 1

Reset to default 12

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";
发布评论

评论列表(0)

  1. 暂无评论