/

How to install "Random Post" (display random posts)

Random post is a way to display post random, random fact, how to install the post are old and many have written, but the bloggers who are just starting a blog was still confused, maybe tutorila used / found to be still using English. In order not to make friends blogger aja disappointed I wrote a brief article.

Easy steps to make Random Post:

First:

Go to Template -> Edit HTML -> Do not forget to do backup template:)

Then copy-paste the code below usually top tags</head>


<script type="text/javascript">

//<![CDATA[

var _yourBlogUrl
= "
http://www.o-om.com";



function randomPost() {

  var script = document.createElement("script");


  script.setAttribute("type", "text/javascript");

  var theUrl = _yourBlogUrl
+"/feeds/posts/summary?alt=json-in-script&callback=getTotalPostsCallback&start-index=1&max-results=1";

  script.setAttribute("src", theUrl);

  document.documentElement.firstChild.appendChild(script);

};


function getTotalPostsCallback(json) {

  var totalResults = json.feed.openSearch$totalResults.$t;

  if (totalResults > 0) {

    getRandomPostNumber(totalResults);

  }

};


function getRandomPostNumber(totalResults) {

  var randomNumber = Math.floor((Math.random() * totalResults) + 1);

  getRandomUrl(randomNumber);

};

function getRandomUrl(randomNumber) {

  var script = document.createElement("script");

  script.setAttribute("type", "text/javascript");


  var theUrl = _yourBlogUrl
+"/feeds/posts/summary?alt=json-in-script&callback=getTheUrlCallback&start-index="
+ randomNumber + "&max-results=1";

  script.setAttribute("src", theUrl);

  document.documentElement.firstChild.appendChild(script);

};

function getTheUrlCallback(json) {

  var theUrl = json.feed.entry[0].link[0].href;


  window.location.href = theUrl;

}

//]]>

</script>

=> Do not forget to replace the red color above with our blog's name, if it do not forget to save


Second:

Then add a page element -> HTML / Javascript

<a href="javascript:randomPost();">View Random Post</a>

saved and looking result


Related Post



RSS Feed

0 Comments for How to install "Random Post" (display random posts)

Leave a comment!

design by tikimedia © 2010 | Converted to Blogger by Blogger Templates | I Powered by Blogger