Last active
July 27, 2020 15:48
-
-
Save amirhp-com/ffaa19639912f587e67aaabe26b5c728 to your computer and use it in GitHub Desktop.
Check if a URL is valid image in JavaScript
This file contains 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
/* | |
* Check if a URL is valid image in JavaScript | |
* | |
* Usage: pass the url to isValid(), if it's a valid image url, | |
* the function will return URL and if not, you'll get false or | |
* whatever you've entered in second parameter | |
* | |
* Developer: Amirhosseinhpv ( https://hpv.im/ ) | |
* Snippet at: 1399-05-06 2020-07-27 19:11 | |
*/ | |
let isValid = function(urlTocheck="", defaultValue=false){ | |
var image = new Image(); | |
image.src = urlTocheck; | |
if (image.width == 0) { | |
return defaultValue; | |
} else { | |
return urlTocheck; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment