Stefan
Stefan

Reputation: 1071

parse url - javascript

I try to parse an URL in angular this way:

var l = document.createElement("a");
l.href='www.example.com';
console.log(l.hostname); // -> '127.0.0.1'

But the log gives me the localhost. Any ideas why that's the case or better ways how to do it?

Upvotes: 0

Views: 32

Answers (1)

alberto-bottarini
alberto-bottarini

Reputation: 1231

You should use an absolute url with protocol:

l.href='http://www.example.com';

Upvotes: 1

Related Questions