Loading...
Loading...
TypeError: Failed to construct 'URL': Invalid URLThe URL constructor threw an error because the string you passed is not a valid URL. This usually happens when a relative URL is passed without a base, or the URL contains invalid characters.
The URL constructor accepts a second argument as the base.
// ❌ Throws for relative URLs
new URL('/api/data');
// ✅ Provide a base
new URL('/api/data', 'https://example.com');
// ✅ Or use window.location as base
new URL('/api/data', window.location.origin);Wrap in try/catch to handle invalid URLs gracefully.
function isValidUrl(str) {
try {
new URL(str);
return true;
} catch {
return false;
}
}Use our URL Encoder to properly encode special characters in your URL.