1

I have the following html page which takes urls from a file and display them. The images are resized to fit the screen to avoid the need to scroll. My problem is it is displayed correctly on firefox but iceweasel just display an empty page. This got me really confused because I did not find anything on it. Thank you for your help.

The images all have a Width 400 and Height 400 but iceweasal states the first image has Width 0 and Heigth 19. If I load the page and refresh it a few times the page is displayed correctly.

The html page:

<!DOCTYPE HTML>
<html>
<head><style>
.col-md-3 img {
    float:left;
}
body {
    margin:0px;
}
</style>
<script>    
var inRowLimit = 3;

function resizeImgs() {
    var images = document.querySelectorAll("img");
    var count = images.length;
    var rows = 1;
    if (count > inRowLimit) {
        rows = Math.ceil(count/3);
        count = inRowLimit;
    }
    var maxWidth = Math.floor(window.innerWidth/count);
    var maxHeight = window.innerHeight/rows;
    for(var i=0; i<=images.length; i++) {
        var currW = document.getElementById('img'+i).offsetWidth;
        var stretchW = maxWidth/currW;
        var currH  = document.getElementById('img'+i).offsetHeight;
        var stretchH = maxHeight/currH;
        if (stretchW <= stretchH)     
            document.getElementById('img'+i).style.width=currW*stretchW+'px';
         } else {
            document.getElementById('img'+i).style.height=currH*stretchH+'px';
        }           
      }
   }

function go() {
    <!--abstracted-->
    loadImgs(file);
}

function loadImgs(infile) {
    <!--abstracted-->
    resizeImgs();
}
window.onload = go
</script>
</head>
<body>
<div id="images">
</div>
</body>
</html>
Charles
  • 50,943
  • 13
  • 104
  • 142
user2196234
  • 71
  • 1
  • 1
  • 8

1 Answers1

0

Why wont you use some css to resize the images? You can make use of css for images: .col-md-3 img { display: block; float:left; width: 33.3333%; max-width: 100%; height: auto; } As far as I unterstand your problem js code for resize is completely unnecessary.

Seems its issue with running code when browsers hasn't copmletely downloaded images. You should execute your code after window load evenet was fired.

https://developer.mozilla.org/en-US/docs/Web/Reference/Events/load

adamiscoding
  • 128
  • 7