javascript - Why use anchor.href property to process url -


i've found following code today:

var basehref = document.createelement('a'); basehref.href = this.baseurl; basehref = basehref.pathname; 

where this.baseurl string.

it seems takes advantage of conversion functionality perform when href set or pathname read. it's assumption. functionality? or other uses can be?

creating anchor, , passing in url, takes advantage of browsers built in parser url's, different parts of url can returned easily, no regular expressions or string manipulation.

the pathname ... well, pathname after protocol , hostname etc.

a small example, of parts can returned way

var basehref = document.createelement('a');  basehref.href = 'https://www.stackoverflow.com:3333/page/that/goes/somewhere?querystring=test#hash';    console.log('protocol    : ', basehref.protocol);  console.log('host        : ', basehref.host);  console.log('hostname    : ', basehref.hostname);  console.log('port        : ', basehref.port);  console.log('pathname    : ', basehref.pathname);  console.log('querystring : ', basehref.search);  console.log('hash        : ', basehref.hash);

this neat trick parse url's using browser.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -