Last Updated: February 25, 2016
·
1.448K
· chinchang

Get URL parts without regex

Here is a neat trick I found on Stackoverflow to get parts of any URL without using regex:

var a = document.createElement('a');
a.href = url;

// Get parts easily now:
console.log(a.host);
console.log(a.hostname);
// and so on...

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside