How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Check the HTTP Referrer with JavaScript
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Check the HTTP Referrer with JavaScript
Web Development

How to Check the HTTP Referrer with JavaScript

how7o
By how7o
Last updated: May 22, 2026
4 Min Read
JavaScript check HTTP referrer — document.referrer
SHARE

To check the HTTP referrer with JavaScript, read document.referrer. It returns the full URL of the page that linked to the current page, or an empty string if the visitor came directly. This guide covers basic detection, social-media source matching, and the privacy-related reasons the referrer is often empty in modern browsers.

Contents
  • TL;DR
  • Using document.referrer
  • Match against an allowlist of sources
  • When the referrer is empty
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 in Chromium, Firefox, and Safari. Originally published 2023-04-09, rewritten and updated 2026-05-17.

TL;DR

const referrer = document.referrer;

if (referrer === '') {
  console.log('Direct visit (no referrer)');
} else if (referrer.includes('facebook.com')) {
  console.log('From Facebook');
} else if (referrer.includes('twitter.com') || referrer.includes('x.com')) {
  console.log('From Twitter/X');
} else if (referrer.includes('instagram.com')) {
  console.log('From Instagram');
} else {
  console.log('From other source:', referrer);
}

Using document.referrer

document.referrer is a read-only string property on the global document object. It’s available in every browser, no library needed:

const referrer = document.referrer;
console.log(referrer);
// "https://www.google.com/"  (or "" for direct visits)
JavaScript check HTTP referrer — document.referrer values for different traffic sources

Match against an allowlist of sources

const sources = {
  'facebook.com':  'Facebook',
  'twitter.com':   'Twitter/X',
  'x.com':         'Twitter/X',
  'instagram.com': 'Instagram',
  'linkedin.com':  'LinkedIn',
  'reddit.com':    'Reddit',
};

const ref = document.referrer;
const matched = Object.entries(sources).find(([host]) => ref.includes(host));
const source  = matched ? matched[1] : (ref === '' ? 'Direct' : 'Other');

console.log('Source:', source);

When the referrer is empty

document.referrer is "" in several common cases — far more often than you’d expect:

  • User typed the URL directly or opened a bookmark.
  • Source page set Referrer-Policy: no-referrer.
  • Cross-origin navigation between HTTPS and HTTP (browser strips for privacy).
  • Visitor opened the link with right-click → “Open in new tab” in some browsers.

For accurate traffic-source attribution, combine the referrer with UTM parameters on the URL — UTMs survive even when the referrer is stripped.

Frequently asked questions

Why is document.referrer empty for many visitors?

Several reasons: the user typed your URL directly, opened it from a bookmark, came from an HTTPS site that didn’t pass the referrer (default browser policy in many cases), or the source site set Referrer-Policy: no-referrer. Modern browsers also strip the path from cross-origin referrers — so even when set, you usually only see the origin (https://www.google.com/), not the search query.

Can I get the search query from document.referrer?

Almost never anymore. Google and most large search engines redirect through their own URL and strip query parameters from the referrer they send. You’ll typically just see https://www.google.com/ with no path. For actual search-term tracking, use Google Search Console (organic) or UTM parameters (paid).

Should I use document.referrer for security checks?

No. The referrer is set by the user’s browser and can be empty, spoofed, or stripped — never trust it for authorization or CSRF protection. For CSRF, use proper anti-forgery tokens. For ‘where did this user come from’ analytics, the referrer is fine because the worst case is bad data, not a bypass.

Is document.referrer different from the HTTP Referer header?

It’s the same value — document.referrer exposes the value the browser sent in the request’s Referer HTTP header (note the historical single-r spelling). The JavaScript property gives you access from the client side without parsing headers.

Related guides

  • How to Check if a JavaScript String Is a Valid URL
  • How to Break Out of a jQuery .each() Loop

References

MDN Document.referrer: developer.mozilla.org/en-US/docs/Web/API/Document/referrer. Referrer-Policy: MDN Referrer-Policy.

TAGGED:JavaScriptjQuery

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Change SSH port on Linux — firewall, SELinux, sshd_config, systemctl restart How to Change the Default SSH Port on Linux
Next Article Check Bootstrap modal open or closed with jQuery How to Check if a Bootstrap Modal Is Open or Closed with jQuery
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026
Tailscale mesh — peer-to-peer connections between devices, coordination server
How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
May 24, 2026

You Might Also Like

Return or throw an error in Laravel (JSON response vs exception)
Web Development

How to Manually Return or Throw an Error Exception in Laravel

6 Min Read
Make the first option selected in a jQuery select
Web Development

How to Make the First Option Selected in a with jQuery

4 Min Read
Laravel foreign key constraint linking posts.user_id to users.id in a schema diagram
Web Development

How to Add Foreign Keys in Laravel Migration

6 Min Read
Laravel nullable exists validation — nullable, sometimes, and present rule combinations
Web Development

Laravel Nullable Exists Validation (nullable, sometimes, present)

9 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?