JavaScript goes here.. and i have many sections of div tag.. i want to make this script common for all divs that i am using.. where as only the url’s have to change reference with the corresponding div.. plz help..??
<script type="text/javascript">
var feedcontainer1=document.getElementById("feeddiv1")
var rssoutput1="<b>TOI</b><br /><ul>"
function rssfeedsetup1(){
google.load("feeds", "1")
var feedpointer1=new google.feeds.Feed("http://handheld.softpedia.com/backend-software.xml") //Google Feed API method
feedpointer1.setNumEntries(10) //Google Feed API method
feedpointer1.load(displayfeed1) //Google Feed API method
}
function displayfeed1(result){
if (!result.error){
var thefeeds1=result.feed.entries
for (var i=0; i<thefeeds1.length; i++)
rssoutput1+="<li><a href='" + thefeeds1[i].link + "'>" + thefeeds1[i].title + " »" + "</a></li>"
rssoutput1+="</ul>"
feedcontainer1.innerHTML=rssoutput1
}
else
alert("Error fetching feeds!")
}
</script>
You need to create a callback function that knows about where to put the data returned from google. In Javascript, the best way to do this is to use a function closure to remember the destination. Therefore, you need a function that creates the callback, such as:
This function can be called multiple times, and each time it will return a function that knows how to populate different divs. You’ll call it something like:
and finally, your setup function needs the callback (and perhaps other parameters):
Overall, this is better separation of concerns as well, so it will be easier to maintain.