Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you refresh a page and persist the Referer header

I need to pragmatically refresh my page (for example location.reload(true);) but also maintain the original http referer header.

the http referer header persists if you click the refresh button on your browser. I know there are a ton of different ways to refresh the page in javasript, is there a way to maintain the http referer?

Thanks!

like image 264
josh Avatar asked May 06 '16 18:05

josh


People also ask

How do you set a referrer header?

It can be done by simply using the HTTP header or the meta element in HTML which takes referrer keyword as value that in turn allows referrer policy setting through markup or using the referrerpolicy content attribute in HTML.

Can you set Referer header?

You cannot set Referer header manually but you can use location. href to set the referer header to the link used in href but it will cause reloading of the page.

Can we change Referer header in Chrome?

The Chrome browser currently does not offer the ability to change referer header settings from a configurations page. However, you can make a quick edit to the Chrome launch options that will disable headers altogether. This is especially useful in Windows if you use a shortcut icon to open the .exe file.


1 Answers

You can't force the value of the document.referrer to be something specific from a reload. But there are workarounds.

A quick, unobtrusive technique is to persist this state in the user's sessionStorage or localStorage.

// Get the last known referrer
const referrer = JSON.parse(localStorage.getItem('myApp')).referrer;

// Save the current referrer.
localStorage.setItem('myApp', JSON.stringify({
    referrer : document.referrer
}));

You could also tie some state to a reload by storing it in a query string or fragment identifier.

const referrer = document.referrer;

if (referrer) {

    let query = location.search;

    if (query) {
        query += '&';
    }

    query += 'referrer=' + encodeURIComponent(referrer);

    location.search = query;  // reloads the page
}

However, if you don't have much control over the page content, the query string approach can be problematic because some applications (front-end and back-end) that see a query will think they need to do something special. So it has a potential for unintended side effects.

like image 116
Seth Holladay Avatar answered Sep 28 '22 08:09

Seth Holladay