Jump to content

Foreach inside Javascript


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 post
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 post
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 post
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 post
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 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...
×
×
  • Create New...