Loading...
function isValidUrl(str) {
try {
new URL(str);
return true;
} catch {
return false;
}
}function isValidUrl(str) {
return URL.canParse(str);
}URL.canParse() gives the same answer as new URL() without throwing or creating an object.
URL.canParse() instead of a regex or a try...catch around new URL().URL.canParse(path, location.origin).Only with a base. URL.canParse('/about') is false, while URL.canParse('/about', 'https://example.com') is true.
No. It checks only the syntax and makes no network request.
Use URL.parse(), which returns a URL object or null instead of throwing, or new URL() inside try...catch.