admin管理员组

文章数量:1415684

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)

本文标签: Get previous URL with javascriptStack Overflow