I want to execute a function only if the previous page the user visited is on a certain domain. How can this be done using javascript (or jQuery or whatever)?
Asked
Active
Viewed 782 times
1
-
4possible duplicate of [How do you get the previous url in Javascript?](http://stackoverflow.com/questions/3528324/how-do-you-get-the-previous-url-in-javascript) – John Flatness Aug 21 '11 at 20:46
3 Answers
1
You can use document.referrer
for this. However, don't count on it always being correct, as some proxies, e.g., don't supply it. It will probably give you some good hits, though.

Erik A. Brandstadmoen
- 10,430
- 2
- 37
- 55
1
You can use javascript's referrer
object to see where the user came from.
if (document.referrer && document.referrer.indexOf("http://" + somehost) > 0){
// do something
}
Be cautions because this not really reliable. You should use some kind of persistent data to know what the user did.

Amir Raminfar
- 33,777
- 7
- 93
- 123