Return to Snippet

Revision: 20107
at November 7, 2009 16:47 by benjamin


Initial Code
function getReferrer() {
  var Refer = document.referrer
  if ( ! Refer ) return false
  if (document.referrer.length > 0 && document.referrer.indexOf("myoutsourcedbrain.com") > -1) return false
  if (document.referrer.indexOf("entrecard.com") > -1) return 2 // 2 for entrecard
  if (document.referrer.indexOf("twitter.com") > -1) return 3 // 3 for twitter
  return 1;  // by default probably search. 
} 

function getQuery(){
  var Refer = document.referrer
  q = Refer.match(/(q(([^=]*)|kw)?|p)=([^"&;]+)/)
  if ( ! q ) return false
  query = unescape( q[ ( q.length - 1 ) ] )
  query = query.replace("/</g,>/g", '&gt;')
  query = query.replace("/\+/g", ' ')
  if ( ! query.match(/\w/) ) return false
  return query
}

function yousearchedfor(result){
  if(result.responseData.cursor.estimatedResultCount>1) {
      var search_again=document.getElementById("welcome_message");
      search_again.innerHTML='<h2>Welcome Searcher.</h2><br/>According to google, for your query "' + query + '" there are <a href="http://www.myoutsourcedbrain.com/search?q=' + query + '">' + result.responseData.cursor.estimatedResultCount + ' results</a> on <i>My Outsourced Brain</i>. Please feel free to leave comments. If you like the site, you might want to subscribe to our feed to stay updated. ';
  }
}

function welcome_entrecard(){
  var search_again=document.getElementById("welcome_message");
  search_again.innerHTML='<h2>Welcome Entrecard Dropper!</h2><br/>I hope you enjoy what you see enough to explore for more than the few seconds it takes to drop your card. My Outsourced Brain features articles about topics ranging from technology and software to scientific research. Recent articles have been more focused on <a href="/search/label/blogger">blogging</a> help, including several useful <a href="/search/label/widget">widgets</a>, which I wrote myself. You can find many articles on <a href="/search/label/linux">linux</a>, phd research, and <a href="/search/label/software">software</a> or of general interest such as lifestyle, and <a href="/search/label/nutrition">nutrition</a>. I write occasional <a href="/search/label/book review">book reviews</a>. <br/>A good place for exploration are most visited articles, labels, the search, or the article archive. <br/>I try to post several articles a week, but life being what it is, sometimes I make it, sometimes not. If you like what you see, it might be worthwhile subscribing to the <a href="/atom.xml?redirect=false&start-index=1&max-results=500">feed</a> in order to stay updated.';
}

function welcome_twitter(){
  var search_again=document.getElementById("welcome_message");
  search_again.innerHTML='<h2>Welcome Tweeter!</h2><br/>I hope you enjoy this site. My Outsourced Brain features articles about topics ranging from technology and software to scientific research. Recent articles have been more focused on <a href="/search/label/blogger">blogging</a> help, including several useful <a href="/search/label/widget">widgets</a>, which I wrote myself. You can also find many articles on <a href="/search/label/linux">linux</a>, phd research, and <a href="/search/label/software">software</a>. You can also find topics of general interest such as lifestyle, and <a href="/search/label/nutrition">nutrition</a>. I write occasional <a href="/search/label/book review">book review</a>s.<br/>A good place for exploration are most visited articles, labels, the search, or the article archive. <br/>I try to post several articles a week, but life being what it is, sometimes I make it, sometimes not. If you like what you see, it might be worthwhile subscribing to the <a href="/atom.xml?redirect=false&start-index=1&max-results=500">feed</a> in order to stay updated.';
}


var refer=getReferrer()
if(refer){  // refer > 0 query true, means the visitor was referred from another web site
  switch (refer)
{ // query is a number that indicates the referral web site
    case 1: // search engine
          qry=getQuery();
          if (qry){
          var script = document.createElement('script');
          script.src = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q='+qry+'+site%3Awww.myoutsourcedbrain.com&callback=yousearchedfor';
          script.type = 'text/javascript';
          document.getElementsByTagName('head')[0].appendChild(script);}
          break;
    case 2: welcome_entrecard(); 
          break;
    case 3: 
          welcome_twitter();
          break;
    default: // standard welcome message
  }
}

Initial URL
http://www.myoutsourcedbrain.com/2009/11/greeting-widget-entrecard-twitter-etc.html

Initial Description
This javascript greets visitors to your website depending on where they come from. There are messages for visitors coming from Entrecard, Twitter, and search engines. It should be easy to adapt the code to include other referral sites as well. 

In the HTML of your page, where you want the message to displayed include 
&lt;div id="welcome_message"&gt;&lt;/div&gt;. 

Please leave comments on my blog for any questions or suggestions.

Initial Title
Visitor Welcome Widget

Initial Tags
twitter

Initial Language
JavaScript