Visitor Welcome Widget


/ Published in: JavaScript
Save to your folder(s)

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
<div id="welcome_message"></div>.

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


Copy this code and paste it in your HTML
  1. function getReferrer() {
  2. var Refer = document.referrer
  3. if ( ! Refer ) return false
  4. if (document.referrer.length > 0 && document.referrer.indexOf("myoutsourcedbrain.com") > -1) return false
  5. if (document.referrer.indexOf("entrecard.com") > -1) return 2 // 2 for entrecard
  6. if (document.referrer.indexOf("twitter.com") > -1) return 3 // 3 for twitter
  7. return 1; // by default probably search.
  8. }
  9.  
  10. function getQuery(){
  11. var Refer = document.referrer
  12. q = Refer.match(/(q(([^=]*)|kw)?|p)=([^"&;]+)/)
  13. if ( ! q ) return false
  14. query = unescape( q[ ( q.length - 1 ) ] )
  15. query = query.replace("/</g,>/g", '&gt;')
  16. query = query.replace("/\+/g", ' ')
  17. if ( ! query.match(/\w/) ) return false
  18. return query
  19. }
  20.  
  21. function yousearchedfor(result){
  22. if(result.responseData.cursor.estimatedResultCount>1) {
  23. var search_again=document.getElementById("welcome_message");
  24. 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. ';
  25. }
  26. }
  27.  
  28. function welcome_entrecard(){
  29. var search_again=document.getElementById("welcome_message");
  30. 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.';
  31. }
  32.  
  33. function welcome_twitter(){
  34. var search_again=document.getElementById("welcome_message");
  35. 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.';
  36. }
  37.  
  38.  
  39. var refer=getReferrer()
  40. if(refer){ // refer > 0 query true, means the visitor was referred from another web site
  41. switch (refer)
  42. { // query is a number that indicates the referral web site
  43. case 1: // search engine
  44. qry=getQuery();
  45. if (qry){
  46. var script = document.createElement('script');
  47. script.src = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q='+qry+'+site%3Awww.myoutsourcedbrain.com&callback=yousearchedfor';
  48. script.type = 'text/javascript';
  49. document.getElementsByTagName('head')[0].appendChild(script);}
  50. break;
  51. case 2: welcome_entrecard();
  52. break;
  53. case 3:
  54. welcome_twitter();
  55. break;
  56. default: // standard welcome message
  57. }
  58. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.