Random Post is a widget that showing a random post from our blog. It’s will assist visitor to find another post that (maybe) have not read before.
I’ll give you an easy and quick tips for making it:
- Choose Edit HTML
- Back up your blog first
- Then copy-paste this code below, put above </head>
<script type="text/javascript">
//<![CDATA[
var _yourBlogUrl = "http://blogtutormaster.blogspot.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>
Change the red code with your blog URL
4. Add a gadget, Choose HTML/Javascript Then copy this code:
<a href="javascript:randomPost();">View Random Post</a>
Save and See the Result. Happy Editing
0 comments:
Post a Comment