JavaScript 判断页面是否是从搜索引擎进入还是直接打开操作

2024-12-09 11:06:34

一. document.referrer

document.referrer 是 JavaScript 中的一个属性,它提供了访问当前页面的来源页面的URL。

document.referrer 是一个只读属性,返回的是浏览器从哪个页面链接访问了当前页面。例如,如果用户点击了一个链接从 A 页面跳转到了 B 页面,那么在 B 页面中 document.referrer 将会返回 A 页面的 URL。

二. 实现代码

const getSearchSite = () => {
  const sites = [
    "google",
    "baidu",
    "soso",
    "bing",
    "360",
    "yahoo",
    "youdao",
    "sogou",
    "gougou",
  ];
  const referrer = document.referrer?.toLowerCase();
  const index = sites.findIndex((i) => referrer.indexOf(i) > -1);
  return index >= 0 ? sites[index] : "";
};

目录

相关推荐
关于调用微软Bing API推送链接时,报:errorcode 3 message ERROR!!! InvalidApiKey如何调用百度和微软必应链接收录APIMac安装nvm时,各种问题总结react-app-rewired中如何设置ant-design主题,以及不生效问题Variable @primary-color-hover is undefinedAnt-Design-Vue Form 中,对于多选下拉表表单时,重置时,表单未正确展示