Parsing URLs on the client side has been common practice for 20 years.Although initially using unreadable regular expressions, the JavaScript specification eventually new URL
How to parse URLs.meanwhile URL
Very useful if a valid URL is provided, but an invalid string will be thrown. new way, URL.canParse
it will be possible to validate URLs.
Provide a malformed URL new URL
Because it throws an error, every time I use new URL
Must be within try/catch
block:
// The correct, safest way try const url = new URL(' catch (e) console.log("Bad URL provided!"); // Oops, these are problematic (mostly relative URLs) new URL('/'); new URL('../'); new URL('/pornhub-interview'); new URL('?q=search+term'); new URL('davidwalsh.name'); // Also works new URL('javascript:;');
As you can see, the string is <a>
Tags are sometimes not attached new URL
.and URL.canParse
can be avoided. try/catch
Determining the validity of a URL is confusing:
// Detect problematic URLs URL.canParse('/'); // false URL.canParse('/pornhub-interview'); // false URL.canParse('davidwalsh.name'); //false // Proper usage if (URL.canParse(' const parsed = new URL('
We’ve come a long way from arcane regular expressions and burners. <a>
elements for this URL
and URL.canParse
API. Nowadays, URLs represent more than location information, so the existence of a reliable API is very useful for his web developer.
welcome to new office
My first professional web development experience was in a small printing shop, sitting in a windowless cube all day. I struggled in that boxed environment for about five years until I was able to find a remote job working from home. first…
Geolocation API
One of the interesting aspects of web development is geolocation. Where are your users viewing your website from? You can set their language locale based on that data, or show them specific products in your store based on their location. Let’s find out how you can…
Create rotation and zoom effects using CSS3
In case you didn’t know, CSS animations are awesome. They are smooth, less burdensome than JavaScript, and are the future of node animation in the browser. Dojo mobile solutions,
dojox.mobile
Now use CSS animations instead of JavaScript to reduce your application’s JavaScript footprint. One of my favorite effects…