I ask you to help understand the error that occurs when working with the collage photo script described in HabrΓ© ( https://habrahabr.ru/sandbox/78613/ )

var collage = document.getElementById('collage'); var images = collage.getElementsByTagName('img'); //МалСнький Π½Π°Π±ΠΎΡ€ ΠΎΠΏΡ†ΠΈΠΉ: averageRowHeight - примСрная высота строки; gap - отступы ΠΌΠ΅ΠΆΠ΄Ρƒ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ°ΠΌΠΈ; var options = {averageRowHeight: 255, gap: 3}; var widths = [], rows = [], rowNumber = 0, index = 0, k = 0; //ЗаписываСм Π² массив ΡˆΠΈΡ€ΠΈΠ½Ρƒ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ ΠΏΡ€ΠΈ ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²ΠΎΠΉ высотС for(var i = 0; i < images.length; i++){ widths.push(Math.round(images[i].width / images[i].height * options.averageRowHeight)); } //Ѐункция суммы элСмСнтов массива Array.prototype.sum = function(){ return this.reduce( function(prev,current){ return +current + prev; }, 0); } //Π Π°Π·Π±ΠΈΠ΅Π½ΠΈΠ΅ ΠΎΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ Π½Π° строки ΠΎΠ±ΠΈΡ‡Π½Ρ‹ΠΌ Π΄Π΅Π»Π΅Π½ΠΈΠ΅ ΠΎΠ±Ρ‰Π΅ΠΉ ΡˆΠΈΡ€ΠΈΠ½Ρ‹ всСх ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΎΠΊ Π½Π° ΡˆΠΈΡ€ΠΈΠ½Ρƒ ΠΊΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€Π° while(rowNumber < Math.ceil(widths.sum() / collage.clientWidth)){ rows[rowNumber] = []; //Как Ρ‚ΠΎΠ»ΡŒΠΊΠΎ общая ΡˆΠΈΡ€ΠΈΠ½Π° ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΎΠΊ прСвысила ΡˆΠΈΡ€ΠΈΠ½Ρƒ ΠΊΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€Π° ΠΏΠ΅Ρ€Π΅Ρ…ΠΎΠ΄ΠΈΠΌ Π½Π° ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΡƒΡŽ строку while(index < widths.length && rows[rowNumber].sum()+(rows[rowNumber].length * options.gap) < collage.clientWidth + options.gap){ rows[rowNumber].push(widths[index]); index++; //Если Π½Π° послСднюю строку ΠΎΡΡ‚Π°Π»ΠΎΡΡŒ ΠΎΠ΄Π½ΠΎ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅, Ρ‚ΠΎ дописываСм Π΅Π³ΠΎ Π² "ΠΏΡ€Π΅Π΄ΠΏΠΎΡΠ»Π΅Π΄Π½ΡŽΡŽ" if(index == widths.length - 1){ rows[rowNumber].push(widths[index]); rowNumber++; break; } } rowNumber++; } index = 0; //НСпосрСдствСнно ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ ΠΌΠ°ΡΡˆΡ‚Π°Π±Π° ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΎΠΊ (ΠΏΡ€ΠΎΠΏΠΎΡ€Ρ†ΠΈΠΎΠ½Π°Π»ΡŒΠ½ΠΎ, Π±Π΅Π· обрСзания ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ) console.log(rows); console.log(rows.length); for(var i = 0; i < rows.length; i++){ for(var j = 0; j < rows[i].length; j++){ // Если ΡˆΠΈΡ€ΠΈΠ½Π° строки большС/мСньшС ΡˆΠΈΡ€ΠΈΠ½Ρ‹ ΠΊΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€Π° - соотвСтствСнно // ΡƒΠΌΠ΅Π½ΡŒΡˆΠ°Π΅ΠΌ/ΡƒΠ²Π΅Π»ΠΈΡ‡ΠΈΠ²Π°Π΅ΠΌ Π΄ΠΎ ΡˆΠΈΡ€ΠΈΠ½Ρ‹ ΠΊΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€Π° с ΡƒΡ‡Π΅Ρ‚Π΅ΠΌ отступов ΠΌΠ΅ΠΆΠ΄Ρƒ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ°ΠΌΠΈ k = (collage.clientWidth - ((rows[i].length - 1) * options.gap)) / rows[i].sum(); images[index].style.width = rows[i][j] * k + 'px'; images[index].style.height = k * options.averageRowHeight + 'px'; if(j < rows[i].length - 1) images[index].style.marginRight = options.gap + 'px'; if(i < rows.length - 1) images[index].style.marginBottom = options.gap + 'px'; index++; } } 

In the Safari browser under mac (possibly in some other), the error undefined is not an object (evaluating 'rows[i].length') on the drain 41

Because of it, Safari blocks the loading of the entire page and the execution of other scripts on it.

Console log: enter image description here

HTML:

  <div id="collage" class="clear"> <a href="/raspoznavaniye-gribov/16290/668ee69f0ad038bbecd791d4c66f0459.jpg" title="480px x 640px" data-gallery=""> <img src="/raspoznavaniye-gribov/16290/668ee69f0ad038bbecd791d4c66f0459.jpg" width="480" height="640"> </a> <a href="/raspoznavaniye-gribov/16290/89c9f637dfe608915c0b40fe0b022246.jpg" title="640px x 480px" data-gallery=""> <img src="/raspoznavaniye-gribov/16290/89c9f637dfe608915c0b40fe0b022246.jpg" width="640" height="480"> </a> <a href="/raspoznavaniye-gribov/16290/e2da464b450dd4942d15df4d6d5dc0b2.jpg" title="640px x 480px" data-gallery=""> <img src="/raspoznavaniye-gribov/16290/e2da464b450dd4942d15df4d6d5dc0b2.jpg" width="640" height="480"> </a> </div> 
  • you in the while missed the assignment of some elements of the rows array due to errors in the logic of the rowNumber++; calls rowNumber++; . - Igor
  • one
    In the debugger did not try to drive? - JK_Action
  • I tried, it seems safari does not like that there is an array in the array - Leonid
  • add the console to the question after calling console.log(rows); before double for - Igor
  • Which line from the question in question has number 36? - Grundy

2 answers 2

In the while missed assigning some elements of the rows array due to errors in the logic of rowNumber++; calls rowNumber++; .

Namely, the assignment of an element with index 1 is missing.

It is striking: rowNumber++; in internal and external while loops - which causes a rowNumber index to rowNumber over a single value if an internal if is executed.

Just in case: break; command break; in your code, only one loop interrupts - the one in which it is directly located.

  • Please tell me how to fix it. The code is not mine. - Leonid
  • one
    @ LeonidOzerne Uh, no. Step-by-step debugging is a personal matter. It is striking: rowNumber++; in internal and external while loops - which causes a rowNumber index to rowNumber over a single value if an internal if is executed. - Igor
  • @Igor, and if break has a label, then it can interrupt several cycles - Grundy
  • @Igor and perhaps this is var widths = [], rows = [], rowNumber = 0, index = 0, k = 0; not assigning an item? - Leonid
  • @ LeonidOzerny is not, rows = [], this is the creation of an array. We are talking about the row rows[rowNumber] = []; . - Igor

Perhaps there is no div on the page with id="collage"

  • No, he is there - Leonid
  • html listing would be nice - OPEX_by
  • Added html code to the question - Leonid