First :
login to Dashboard -> open Template -> Edit HTML -> dont forget to backup the template first :)
Then copy the code below and paste right over the tag </head>
<script type="text/javascript">
//<![CDATA[
var _yourBlogUrl = "http://editing-blog.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>
Do not forget to change the color red with your blog URL, then do not forget saved.
Second :
Then add a page Element -> HTML / Javascript
<a href="javascript:randomPost();">View Random Post</a>
Do not forget to save and see the results :)
source : http://www.o-om.com/2007/12/cara-pasang-random-post-menampilkan.html
Post a Comment