Jump to content
Sign in to follow this  
kareem_elzeiny

Select a random link from an atom rss feed

Recommended Posts

Hello Guys,I have this code which creates a text that redirects to a random post for a given feed URL ( For blogger blogs )

<div id="myLuckyPost"></div><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 = 'http://googleblog.blogspot.com/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 = 'View Random Post'; document.getElementById('myLuckyPost').appendChild(a); }</script> <script src="http://googleblog.blogspot.com/feeds/posts/summary?max-results=0&alt=json-in-script&callback=feelingLucky"></script>

an you tell me how to use this generated random link individually as a variable.(ex. I don't want to create a text that redirect me to the random post, I want to put it in a variable form to use it in another script ) Like This:

var randompost = random post url here;

any help or tip would be appreciated :)Regards,

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...