URL.canParse


Parsing of URLs on the consumer facet has been a standard observe for 20 years. The early days included utilizing illegible common expressions however the JavaScript specification ultimately advanced right into a new URL technique of parsing URLs. Whereas URL is extremely helpful when a sound URL is offered, an invalid string will throw an error — yikes! A brand new technique, URL.canParse, will quickly be accessible to validate URLs!

Offering a malformed URL to new URL will throw an error, so each use of new URL would should be inside a strive/catch block:

// The proper, most secure means
strive {
  const url = new URL('https://davidwalsh.identify/pornhub-interview');
} catch (e) {
  console.log("Unhealthy URL offered!");
}

// Oops, these are problematic (largely relative URLs)
new URL('/');
new URL('../');
new URL('/pornhub-interview');
new URL('?q=search+time period');
new URL('davidwalsh.identify');

// Additionally works
new URL('javascript:;');

As you may see, strings that may work correctly with an <a> tag typically will not with new URL. With URL.canParse, you may keep away from the strive/catch mess to find out URL validity:

// Detect problematic URLs
URL.canParse('/'); // false
URL.canParse('/pornhub-interview'); // false
URL.canParse('davidwalsh.identify'); //false

// Correct utilization
if (URL.canParse('https://davidwalsh.identify/pornhub-interview')) {
  const parsed = new URL('https://davidwalsh.identify/pornhub-interview');
}

We have come a good distance from cryptic regexes and burner <a> parts to this URL and URL.canParse APIs. URLs symbolize a lot greater than location nowadays, so having a dependable API has helped net builders a lot!