If I search for something on google and click on a result (mytestsite.com), the referer to that site will be URL of the google search.
Now on that site, there is a J
A script tag will always refer to the document that is sourcing it. If you're doing something special on the server you might want to consider using a session or cookies.