简体   繁体   中英

Javascript: if (previous page == certainurl) { function }

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)?

如何引用document.referrer

You can use document.referrer for this. However, don't count on it always being correct, as some proxies, eg, don't supply it. It will probably give you some good hits, though.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM