madsrh Posted March 23, 2013 Share Posted March 23, 2013 (edited) 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 Edited March 23, 2013 by madsrh Link to comment Share on other sites More sharing options...
justsomeguy Posted March 25, 2013 Share Posted March 25, 2013 What are you trying to loop through? Are you supposed to be saving those new objects as variables? Do you have a reference for that object? Link to comment Share on other sites More sharing options...
jeffman Posted March 25, 2013 Share Posted March 25, 2013 (edited) 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 March 25, 2013 by Deirdre's Dad Link to comment Share on other sites More sharing options...
madsrh Posted March 26, 2013 Author Share Posted March 26, 2013 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 More sharing options...
justsomeguy Posted March 27, 2013 Share Posted March 27, 2013 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 More sharing options...
madsrh Posted March 27, 2013 Author Share Posted March 27, 2013 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 More sharing options...
justsomeguy Posted March 27, 2013 Share Posted March 27, 2013 Javascript doesn't have anything to do with PHP. Like I said, it starts with "rss0", not "rss1", so it may hit an error if that element doesn't exist which stops the code from continuing. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now