Search code examples
javascriptreferrerhttp-referer

Set Referrer value on a following called url


I have an Html file containing the following code:

<script>
    Object.defineProperty(document, "referrer", {get : function(){ return "myreferrer.com"; }});
    //document.location="somelink.com";
</script>

From what I've read,maybe the thing I'm trying cannot be done,but I wanted to be sure.

I want to visit the site somelink.com but when my browser finishes the redirection to the location,the document.referrer value to be "myreferrer.com".

If I run the html with this format(document.location in comments) the command in url --> javascript:alert(document.referrer) is the one I want. But if I erase the comments and activate the document.location line,the above command will show up an empty document.referrer and not the one I want.

Can I achieve what I have in mind?


Solution

  • Some browser versions allowed you to customize the referer header using the approach of overriding the document.referer property in javascript, but that doesn't appear to be reliable. Even if some browsers still allow that, there's no guarantee it would work in future versions.

    If you need a workaround, you could link to the desired referrer domain and serve up an intermediate page that performs the navigation to the final destination URL via an HTML form submission. That should preserve that intermediate page as the referrer.

    Within the context of a browser extension however, you can alter the headers via onBeforeSendHeaders