Created
May 7, 2013 19:25
-
-
Save huned/5535404 to your computer and use it in GitHub Desktop.
get the biggest image on a page with a reasonable aspect ratio with javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var biggestImage = function() { | |
| biggest = null; | |
| images = document.getElementsByTagName('img'); | |
| for (var i = 0; i < images.length; i++) { | |
| var image = images[i]; | |
| var area = image.width * image.height; | |
| var ratio = image.width / image.height; | |
| if(!biggest || (area > 1 && area > (biggest.width * biggest.height) && ((ratio > 1 && ratio < 2) || (1/ratio > 1 && 1/ratio < 2)))) | |
| { | |
| biggest = image; | |
| } | |
| } | |
| return biggest; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment