kareem_elzeiny Posted August 5, 2011 Share Posted August 5, 2011 Hi,,How can I use Javascript to redirect a visitor to a RANDOM URL from a given RSS feed link (eg. "http://feeds.feedburner.com/blogger/WZDh" )Regards,, Link to comment Share on other sites More sharing options...
jeffman Posted August 5, 2011 Share Posted August 5, 2011 You'd want to use AJAX, but then you'd run against the same-origin policy. I think an iFrame solution would fail for the same reason. So I'm not sure you can. (Am I correct that the feed does not share the same domain as your document?)If you want to use PHP, it's easy, AND I have the code ready. Depending on your exact needs, it can be done in 10 lines or less. Link to comment Share on other sites More sharing options...
kareem_elzeiny Posted August 5, 2011 Author Share Posted August 5, 2011 Actually I'm using Blogger platform which doesn't give me a lot of options, so I can only use HTML & Java ScriptI found This script which redirect the visitor to a random post in the same blog <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 = '/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="/feeds/posts/summary?max-results=0&alt=json-in-script&callback=feelingLucky"></script> But I need to redirect the visitor to a random post in another blog, by changing the feed src it doesn't make any differenceI need to apply it on a specific RSS Feed, Any Ideas ?? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.