0

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

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

4 Comments

And now we look at the syntax highlighting and fix the quote issue. :)
@ThiefMaster: Indeed we do ;)
It will be good to check if document.referer exists too, because it can be undefined
@antyrat: In what browser/ situation? All the docs say it'll be an empty string?

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.