Skip to content

Instantly share code, notes, and snippets.

@luisenriquecorona
Created May 21, 2020 23:17
Show Gist options
  • Save luisenriquecorona/a797e36ced3bbc3554f14ba32f75c11b to your computer and use it in GitHub Desktop.
Save luisenriquecorona/a797e36ced3bbc3554f14ba32f75c11b to your computer and use it in GitHub Desktop.
A smarter scripter would recognize that the preceding scripts might fail in very old browsers, where script errors tend to be rather invasive. To prevent such errors, the author makes two modifications. First, the potentially offending script statements in the getImgAreas( ) function wrap themselves in an object detection block. Second, the main…
function getImgAreas( ) {
var result;
// make sure browser supports img element objects
if (document.images) {
// initialize return value so we can add to it
result = 0;
// loop through all img objects on the page
for (var i = 0; i < document.images.length; i++) {
// accumulate image areas
result += (document.images[i].width * document.images[i].height);
}
}
// returned value is either a number or null
return result;
}
function reportImageArea( ) {
// grab result so we can verify it
var imgArea = getImgAreas( );
var output;
if (imgArea == null) {
// message for browsers not supporting img object
output = "Unknown";
} else {
output = imgArea;
}
document.reportForm.imgData.value = output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment