Tuesday, January 3, 2012

Linking to Sites Randomly

T he other day, I was involved in a discussion about providing links to more than just Amazon.com. The claim was that Barnes & Noble checks to see if authors are linking to them or not and they will stop selling the author’s books if they don’t find links to their site. I don’t know if that is true or if it is just an urban legend. It seems to me that Barnes & Noble has better things to do than to pay someone to see how many links there are from the author’s website. Besides which, Barnes & Noble makes money from selling books, not from links. But it’s one of those things that sounds almost believable because Barnes & Noble has a history of refusing to sell some books. But this post isn’t about that.

In the discussion, one of the things mentioned was that with so many booksellers out there, having more than one link results in a big long list. I suggested that rather than having a list of links, people could just have their website or blog randomly choose which link to provide. One person might visit the site and see a link to Amazon.com while someone else would see a link to Barnes & Noble or one of the other booksellers. The person I said that to said that it was it was beyond his capability. It is actually very simple and I have provided the code below. Instead of the normal links you would put in, this code would go in its place:

<script type="text/javascript">
var numberOfLinks = 2;
var randNum = Math.floor(Math.random()*numberOfLinks);

var linkStr0 = "http://www.amazon.com/Extending-Art-Illusion-Reference-Creating/dp/1612950027";
var linkStr1 = "http://www.barnesandnoble.com/w/extending-art-of-illusion-timothy-fish/1037325198?ean=9781612950020&itm=1&usri=extending+art+of+illusion";

switch (randNum){
case 0:
document.write("<a href=\""+linkStr0+"\">Buy on Amazon.com</a>");
case 1:
document.write("<a href=\""+linkStr1+"\">Buy on BN.com</a>");
document.write("<a href=\""+linkStr0+"\">Buy My Book</a>");

I have put in bold the things that you would need to change for your own website. You may also need to add cases, if you want to link to more than two sites. The link below is the result of this script:

Any questions?