문제

I have added a random post button to my site (http://factowy.blogspot.com). I want to load random post as the page loads instead of clicking the button. How can I do that? Here is the code for the random post button.

<script type="text/javascript"> 
function showLucky(root){ var feed = root.feed; var entries = feed.entry || []; var entry = feed.entry[0]; for (var j = 0; j < entry.link.length; ++j){if (entry.link[j].rel == 'alternate'){window.location = entry.link[j].href;}}} function fetchLuck(luck){ script = document.createElement('script'); script.src = '/feeds/posts/summary?start-index='+luck+'&max-results=1&alt=json-in-script&callback=showLucky'; script.type = 'text/javascript'; document.getElementsByTagName('head')[0].appendChild(script); } function feelingLucky(root){ var feed = root.feed; var total = parseInt(feed.openSearch$totalResults.$t,10); var luckyNumber = Math.floor(Math.random()*total);luckyNumber++; a = document.createElement('a'); a.href = '#random'; a.rel = luckyNumber; a.onclick = function(){fetchLuck(this.rel);}; a.innerHTML = 'Random Fact'; document.getElementById('abt-random').appendChild(a); } </script><script src="/feeds/posts/summary?max-results=0&alt=json-in-script&callback=feelingLucky">
</script>
도움이 되었습니까?

해결책 2

I was able to load random post with the following code:

window.onload = function(){fetchLuck(a.rel);}

다른 팁

You need something like this.

<body onload='showLucky()'>

Go to Blogger > Layout > Edit HTML and replace <body> with above mentioned code and check.

It's just way you would call function on page load. I haven't tried with your function.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top