简体   繁体   中英

How to redirect js file with Javascript based on referer

I have a website mydomain.com

I want,

  • If visitor mydomain.com come from images.google.com then call otherdomain.com/file1.js

  • If visitor come from another domain, then call otherdomain.com/file2.js

What sort of script should I be using on my domain to do this?mydomain.com

The only option you've got is to use document.referrer ; but it is not reliable or trustworthy.

When available, document.referrer will give you the come-from URI as a string; if it's not available it'll be empty ( "" ).

<script>
    if (document.referrer.indexOf('http://images.google.com/') === 0) {
        document.write('<script src="http://otherdomain.com/file1.js"><\/script>');
    } else {
        document.write('<script src="http://otherdomain.com/file2.js"><\/script>');
    }
</script>

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