Jump to content

Foreach inside Javascript


madsrh

Recommended Posts

Hi Can someone tell me how to do a foreach and increase the "rss1" number inside this javascript?

 <script type="text/javascript">	function load() {	   var feed ="http://www.dr.dk/nyheder/service/feeds/allenyheder";	  new GFdynamicFeedControl(feed, "rss1");  var feedtwo ="http://feeds.feedburner.com/d0od?format=xml";	  new GFdynamicFeedControl(feedtwo, "rss2");	   var feedtwo ="http://www.engadget.com/rss.xml";	  new GFdynamicFeedControl(feedtwo, "rss3");  var feedtwo ="http://feeds.newzmedia.dk/c/32893/f/582669/index.rss";	  new GFdynamicFeedControl(feedtwo, "rss4");	}	google.load("feeds", "1");	google.setOnLoadCallback(load);  </script>

I also need the var feed which is the RSS url set in this widget http://pastie.org/7088004 Any help is appreciated :fool:

Edited by madsrh
Link to comment
Share on other sites

It's a Google API. I disapprove of the programming style, but it looks like OP is doing it right. I think. Anyway, I think the question gets answered like this:

var feedArray = [   "http://www.dr.dk/nyheder/service/feeds/allenyheder",   "http://feeds.feedburner.com/d0od?format=xml",   "http://www.engadget.com/rss.xml",   "http://feeds.newzmedia.dk/c/32893/f/582669/index.rss"];for (var i = 0, len = feedArray.length; i < len; i++) {   new GFdynamicFeedControl(feedArray[i], "rss" + i);}

Now you can add an arbitrary number of addresses to the array without changing the rest of the code.

Edited by Deirdre's Dad
Link to comment
Share on other sites

It's a Google API. I disapprove of the programming style, but it looks like OP is doing it right. I think. Anyway, I think the question gets answered like this:
var feedArray = [   "http://www.dr.dk/nyheder/service/feeds/allenyheder",   "http://feeds.feedburner.com/d0od?format=xml",   "http://www.engadget.com/rss.xml",   "http://feeds.newzmedia.dk/c/32893/f/582669/index.rss"];for (var i = 0, len = feedArray.length; i < len; i++) {   new GFdynamicFeedControl(feedArray[i], "rss" + i);}

Now you can add an arbitrary number of addresses to the array without changing the rest of the code.

Thanks for the reply. I don't know why, but this didn't work. I looks like all the DIVs have the ID rss1. I'm trying to add a Wordpress RSS widget (http://stackoverflow.com/questions/15560865/output-url-to-wordpress-widget). So the URLs should be PHP variables from $lineOne.
Link to comment
Share on other sites

That Javascript code doesn't set IDs on divs, it is supposed to be passing IDs of existing elements to put the RSS content into. That loop will keep incrementing i and pass each ID to the constructor, starting at 0 (rss0, rss1, rss2, etc). You need to have those elements already on the page, or add code to the loop to create and append the elements before trying to load RSS content into them.

Link to comment
Share on other sites

That Javascript code doesn't set IDs on divs, it is supposed to be passing IDs of existing elements to put the RSS content into. That loop will keep incrementing i and pass each ID to the constructor, starting at 0 (rss0, rss1, rss2, etc). You need to have those elements already on the page, or add code to the loop to create and append the elements before trying to load RSS content into them.
Sorry, I see that now. But when I set the DIV with ID="rss1" the content doesn't show up :boredom:Does the ID only work inside the JS scope? I really don't understand why it doesn't show up inside the DIV in my PHP function.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...