URL.canParse()
The URL.canParse() static method of the URL interface returns a boolean indicating whether or not an absolute URL, or a relative URL combined with a base URL, are parsable and valid.
This is a fast and easy alternative to constructing a URL within a try...catch block. It returns true for the same values for which the URL() constructor would succeed, and false for the values that would cause the constructor to throw.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 120 | 120 | 115 | 17 | 120 | 17 | |
1+Supported (version) Not supported ※Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Syntax
JAVASCRIPT
const logElement = document.getElementById("log");
function log(text) {
logElement.innerText += `${text}\n`;
} Use cases
-
Using URL.canParse()
The URL.
Cautions
- May not be supported in older browsers.
Implementation notes
- Some APIs require a secure context (HTTPS) or user activation. Check the requirements before use.