Javascript behaves differently on Mac and PC? I know it shouldn't

I created a pagination script that takes a long block of text and paginates it. First, the text is loaded into a div with id #page. The script then measures the height of the #page and calculates how many pages it should be split into nesting in a div with a .detailsholder class.

The div.detailsholder is removed and the corresponding number of sections are added inside it. (Each actually has all the text # of the page inside, but the top margin is set non-agent-wise, the height is fixed, and the overflow is set to hidden, so only the corresponding amount appears.)

And it works great except for this: while Safari and Firefox on Mac work great, IE and Firefox on Windows add an extra page. Because of the way the pages are created, as described in parentheses above, the last page appears to be empty - the text has moved too far to appear in the page window.

Here is the code. I am using jQuery as you can see.

var descHeight = $('#page').outerHeight();
if (descHeight > 250 ) {
   var numberOfPages = Math.round(descHeight/210)+1; //Figure out how many pages
   var artistText = $('#page').html();               //Grab the text into a variable
   $('.detailsholder').empty();                      //Empty the container so we can fill
                                                     //it with pages
   for (i=0;i<=numberOfPages-1;i++) {                //Each page has the entire text
                                                     //content, but is shifted up and
      var shiftUp = 0-(210 * i);                     //cut off at the bottom.
      $('.detailsholder').append('<div id="page' + (i+1) + '" class="page" style="height:225px;"><div style="border:dotted 1px red;margin-top:' + shiftUp + 'px"' + artistText + '</div>');
}

      

}

Thanks!

+1


a source to share


4 answers


I see one problem with the algorithm. Math.round must be Math.floor to give the correct page count. To understand why, suppose descHight is 400. Then you only need 2 pages of height 210 each, but Math.round (400/210) + 1 == 3. Maybe a combination of this problem together with another standard size of a font between platforms (which will likely affect descHight and provoke a problem), is the cause of the observed behavior?



+1


a source


Have you tried it on similar platforms with different resolution / font settings?

I think this issue may have more to do with the display settings and the CSS part of your Javascript and then platform differences. My CSS isn't great, but you might want to avoid px and try to use% instead.



The following steps can help diagnose:

+3


a source


Add a few warnings to see how the calculations work out. If you get different results in different browsers, you will have to make adjustments to handle the differences.

+1


a source


In the last line of the script, the second div tag is opening: it seems like it is not closed. Maybe this is causing the problem?

+1


a source







All Articles