Languages
[Edit]
EN

JavaScript - detect if user entered webpage directly (source: direct)

5 points
Created by:
Imaan-Morin
1009

In this short article, we would like to show you how using JavaScript (in web browser) detect if user entered webpage directly.

As user who entered webpage directly we understand there wasn't used referral link from other webpage.

Quick solution:

// ONLINE-RUNNER:browser;

const isDirect = () => !document.referrer;

console.log(isDirect());

 

Note:

It is impossible to be sure if source is direct entrance when document.referrer is empty depending on possible rel="noreferrer" attribute used in the source link.

This is reasone why empty document.referrer is marked as Direct / (None) in different analytics tools, so it may be good to use UTM links to detect source if it is possible.

 

See also

  1. JavaScript - get source page URL (source: referrer)

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join