检测页面是否被重定向或直接加载(Javascript) [英] Detect if page was redirected or loaded directly(Javascript)

查看:110
本文介绍了检测页面是否被重定向或直接加载(Javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我想在用户访问的网站首页上显示标签。

Currently, I want to display a label on the first page of my site a user visits.

为简单起见,说我只关心page1.html和page2.html。

To make it simple, say I only care about page1.html and page2.html.

是吗可以检查用户是否从page1.html重定向到page2.html?(也许带有cookie?)

Is it possible to check if the user was redirected from page1.html to page2.html?(Perhaps with cookies?)

编辑:
在page1.html上,我设置一个cookie,它将在一分钟后过期。在page2.html上,我检查了cookie。效果很好!

On page1.html, I set a cookie, which would expire in a minute. On page2.html, I checked for the cookie. Works great!

推荐答案

您可以使用 document.referrer

如果用户直接导航到页面(而不是通过链接),则该值为空字符串。

The value is an empty string if the user navigated to the page directly (not through a link).


  1. 例如,当用户通过书本链接访问URL时。

  2. 用户在地址栏中输入完整的URL。




参考 https://developer.mozilla.org/zh-CN/docs/Web/API/Document/referrer

这篇关于检测页面是否被重定向或直接加载(Javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆