Google Answers Logo
View Question
 
Q: display html based on referrer javascript ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: display html based on referrer javascript
Category: Computers > Algorithms
Asked by: questioner1234-ga
List Price: $5.00
Posted: 13 Jun 2004 10:52 PDT
Expires: 13 Jul 2004 10:52 PDT
Question ID: 360419
I would like a javascript that will add some html to a page only if
the visitor is referred from a search engine. So if they come from
yahoo or google, they will see an advertisment that other visitors
won't see. I want the ad to be integrated into the page, not a frame
or pop-up.

Request for Question Clarification by aceresearcher-ga on 13 Jun 2004 20:30 PDT
Greetings, questioner!

Is the advertisment to be inserted an image with a hyperlink, just an
image, or is it to be text?

Regards,

aceresearcher

Clarification of Question by questioner1234-ga on 13 Jun 2004 21:45 PDT
There should be an image with a hyperlink as well as text with a
hyperlink, perhaps quite a bit of text.
Answer  
Subject: Re: display html based on referrer javascript
Answered By: palitoy-ga on 14 Jun 2004 06:57 PDT
Rated:5 out of 5 stars
 
Hello Questioner1234

This is the script you require:

<script language="JavaScript"><!--
if (document.referrer.indexOf('google.com') > -1)
    document.write('this is where you write out the html to be
displayed - remember to escape any quotes!');
else if (document.referrer.indexOf('yahoo.com') > -1)
    document.write('this is where you write out the html to be
displayed for the yahoo visitors');
else 
   document.write('otherwise this gets shown - could be nothing');
//-->
</script>

As it is shown here it should be placed in between the <head> tags of
your document and will write a phrase to the browser should it detect
that google.com or yahoo.com was the referrer.  Please note that some
security settings block this setting.

The document.write sections write the code that will display a message
or your advert html code.

If you have any questions regarding this please ask for clarification
and I will do all I can to help.

Request for Answer Clarification by questioner1234-ga on 14 Jun 2004 07:56 PDT
Thanks so much. What if I want to display the text somewhere other
than the top of the screen? How do I choose where to display the text
in the page's HTML? Thanks.

Clarification of Answer by palitoy-ga on 14 Jun 2004 08:04 PDT
To display the text elsewhere in the page you should assign the html
you wish to add to a variable.

The variable can then be printed out elsewhere on the page.

This would be done something like this:

<html>
<head>
<script language="JavaScript"><!--
if (document.referrer.indexOf('google.com') > -1)
    $var = 'this is where you write out the html to be
displayed - remember to escape any quotes!';
else if (document.referrer.indexOf('yahoo.com') > -1)
    $var = 'this is where you write out the html to be
displayed for the yahoo visitors';
else 
   $var = 'otherwise this gets shown - could be nothing';
//-->
</script>
</head>
<body>
<script>document.write('' + $var);</script>
</body>
</html>
questioner1234-ga rated this answer:5 out of 5 stars

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy