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
Run Laravel queue workers with Supervisor
How to Run Laravel Queue Workers in Production with Supervisor
May 23, 2026
Nginx as a reverse proxy for a Node.js app on Ubuntu
How to Set Up Nginx as a Reverse Proxy for Node.js on Ubuntu
May 23, 2026
Install and configure Redis on Ubuntu for Laravel and WordPress
How to Install and Configure Redis on Ubuntu (for Laravel & WordPress)
May 23, 2026
Harden a fresh Ubuntu VPS with UFW, Fail2Ban, and SSH key auth
How to Harden a Fresh Ubuntu VPS: UFW + Fail2Ban + SSH Key Auth
May 23, 2026
Set up Let's Encrypt SSL with Certbot on Ubuntu
How to Set Up Let’s Encrypt SSL with Certbot on Ubuntu (Apache & Nginx)
May 23, 2026

You Might Also Like

Install Node.js on Ubuntu — terminal with NodeSource setup_22.x curl command and Node.js hexagon icon
Web Development

How to Install Node.js on Ubuntu (22.04 & 24.04): Step-by-Step

11 Min Read
DataTables server-side Ajax pagination with Laravel
Web Development

How to Create Ajax-Based Pagination in DataTables

6 Min Read
WordPress default posts per page — get_option reads the Settings Reading value
Web Development

How to Get the Default Posts Per Page Value in WordPress

5 Min Read
React.createElement conditional rendering with && short-circuit
Web Development

Conditional Rendering with React.createElement

6 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?