最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get previous URL with javascript - Stack Overflow

programmeradmin0浏览0评论

I am looking for a solution with javascript to check if the previous page is not extranal.

With this check I want return true or false so I can use this for another function.

What I have tried is to get the previous URL with history.go(-1) but this is undefined.

I am happy if I can get the previous URL so far.

Hope somebody can help me out with this.

I am looking for a solution with javascript to check if the previous page is not extranal.

With this check I want return true or false so I can use this for another function.

What I have tried is to get the previous URL with history.go(-1) but this is undefined.

I am happy if I can get the previous URL so far.

Hope somebody can help me out with this.

Share Improve this question edited Jun 15, 2023 at 8:04 Penny Liu 17.6k5 gold badges86 silver badges108 bronze badges asked Feb 2, 2022 at 23:19 BenBen 731 gold badge1 silver badge7 bronze badges 1
  • 1 In JS it's document.referrer. – CherryDT Commented Feb 2, 2022 at 23:23
Add a ment  | 

2 Answers 2

Reset to default 1

Simply use to know previous URL

document.referrer

 document.write(document.referrer);

You can use document.referrer to get the URL of the previous page.

To check whether it is external or not, you can parse it with the URL constructor and pare its origin.

const previousURL = document.referrer;

const isPreviousURLExternal = new URL(previousURL).origin == new URL(location).origin;
console.log(isPreviousURLExternal)

发布评论

评论列表(0)

  1. 暂无评论