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

javascript - How do you refresh a page and persist the Referer header - Stack Overflow

programmeradmin13浏览0评论

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!

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!

Share Improve this question asked May 6, 2016 at 18:17 joshjosh 1,2711 gold badge13 silver badges32 bronze badges 1
  • Possible duplicate of How to manually set REFERER header in Javascript? – Alon Eitan Commented May 6, 2016 at 18:19
Add a ment  | 

1 Answer 1

Reset to default 5

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.

发布评论

评论列表(0)

  1. 暂无评论