Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Hossam Khalifa
17,200 PointsJavascript looping problem
This is some code that loops through an array containing links that should be opened in a new window The following code starts opening the first window while adding it as an objects in the "windows" empty object.
var link = document.querySelectorAll("a.title");
var windows = {};
windows["wind" + 0 ] = window.open(link[1].href);
windows["wind" + 0 ].onload =function(){
windows["wind" + 0 ].location.href = $(windows["wind" + 0 ].document.getElementsByClassName("rss-link")).attr("href");}
var i = 1;
This is the function that should be done every loop and the loop
function download () {
windows["wind" + (i-1) ].onload = function(){
windows["wind" + i ] = window.open(link[i].href);
windows["wind" + i ].onload = function(){
windows["wind" + i ].location.href = $(windows["wind" + i ].document.getElementsByClassName("rss-link")).attr("href");
}
}
}
while(i<link.length){
download();
i++;
}
This the error I get : Uncaught TypeError: Cannot set property 'onload' of undefined
And the windows object looks like this
windows = {wind0: Window, wind2: Window}
Please help
3 Answers
Hossam Khalifa
17,200 PointsAny answers ??
Vittorio Somaschini
33,371 PointsHello Hossam.
I am having troubles understanding your question, but a couple of things that you can do may help me.
1) on which line is the error (according to javascript console)?
2) can you please write an example of how you want your output to be?
ty
Vittorio
Hossam Khalifa
17,200 PointsThe error is on the first onload event in the download function And the output should be windows that get opened after the other one have finished loading and the links are in the links array on line 1
Hossam Khalifa
17,200 PointsThe error is on the first onload event in the download function And the output should be windows that get opened after the other one have finished loading and the links are in the links array on line 1
Jeff Everhart
21,732 PointsI don't think your windows object is valid JSON. You need quotation marks around the keys and values: http://json.org/example
Hossam Khalifa
17,200 PointsThe windows object looks like this after the loop has finished I mean it does not create a wind1 property of the windows object and I don't know why