0

I'm using a third part script (Chaordic loader.js for product recommendations) on my site and it is overriding document.referrer with rubbish, causing me a headache.

Question: Is there a way to prevent this modification? Can I "freeze/seal" document.referrer in some way?

I know I could just cancel the contract with the company but would like to try this lock first. I can put javascript code before or after the third part code, but can't modify it.

Third part code that overrides document.referrer:

var t = window.chaordic && window.chaordic.readCookie && window.chaordic.readCookie(e);
Object.defineProperty(window.document, "referrer", {
  configurable: false,
  get: function() { return t }
});
lcrespilho
  • 800
  • 5
  • 10

2 Answers2

0

If you are in control of the document that takes you to the page that is messing with document.referrer, make sure any/all links are set up with the rel="noreferrer" attribute, which tells the current window not to keep a reference of itself as it navigates to the next window, making document.referrer come back as null.

As I'm not sure if you have access to the calling page or how exactly you are winding up on the target page, I didn't mark your question as a duplicate, but there is more information here.

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • Hi Scott. Thank you for the answer. In fact my website is a ecommerce and I need do be able to see the referrals that brought traffic to my website. The Ehab's solution solved my problem. Thanks! – lcrespilho Sep 12 '19 at 02:42
0

Well the answer almost lies in your question

// You need to put the following code before their code runs 
const referrer = document.referrer;
Object.defineProperty(window.document, "referrer", {
  configurable: false,
  get: () => referrer,
});

You can also do it like this

Object.defineProperty(window.document, "referrer", {
  configurable: false,
  writable: false,
  value: document.referrer
});

Now aside from Javascript, I would consider other options. changing the document.referrer is one nasty thing they are doing that you are aware of. They could be doing other nasty things that you are not aware of.

ehab
  • 7,162
  • 1
  • 25
  • 30
  • Wow! I loved the second solution. Thank you very much! The first one worked too, but I didn't understand the need of the `set` method. Tried to set `document.referrer = 'sad'` like your example but it didn't change document.referrer value. I found it worked the same without the `set` method. Maybe I did something wrong. Anyway, thank you again! – lcrespilho Sep 12 '19 at 02:18
  • Yea a setter property is indeed not needed, omitting it will also prevent writing to the property. I edited my answer. Also if you like the answer others reading this question can find it easier (many people depend on the number of votes when picking their answer). – ehab Sep 12 '19 at 04:53
  • I've tried to like the answer, but since I don't have reputation the like is not displayed. :( This is the message that appears when I try to like: "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." – lcrespilho Sep 12 '19 at 12:55