0

Is there a way to detect the previous URL that user visited before arrive on my website using javascript or jQuery?

I hope it can be possible avoid using PHP

1
  • You should refer to the w3 spec for what the use cases are for being able to retrieve that information (whether on the server or on the browser) and what the values might be. Commented Jul 14, 2015 at 20:22

2 Answers 2

1

You seem to want to get the previous URL when the user directly enters the new URL in the browser without clicking a link. This is not possible in PHP or JavaScript. It would be a significant and quite unnecessary security risk, so browsers will refuse to send it. You can only get it through the referrer if the user clicked a link on the previous website that redirected them to yours.

Frankly, there should be no reason for the new website to know about the previous one unless the user was referred from the previous URL by clicking a link.

Sign up to request clarification or add additional context in comments.

4 Comments

what you mean "because it would be a huge security risk" please can you explain better in your answer?
I mean that websites could track you unwillingly even though this feature would provide no applicable value. The website you just happened to previously be on is in no way relevant to the new one.
So how commonly people do it? because I know that other websites can track the previous page
They don't. If anything, they track a referrer which is only present when people click a link on the previous page to be redirected to the new one.
1
document.referrer

I think is the most common one. There is also the history api

1 Comment

seem to does not work for me... I'm using Chrome on Windows

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.