Skip to content

Instantly share code, notes, and snippets.

@amirhp-com
Last active July 27, 2020 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirhp-com/ffaa19639912f587e67aaabe26b5c728 to your computer and use it in GitHub Desktop.
Save amirhp-com/ffaa19639912f587e67aaabe26b5c728 to your computer and use it in GitHub Desktop.
Check if a URL is valid image in JavaScript
/*
* 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