Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to easily dissect a link with javascript?

How would I go about dissecting a link/href with Javascript or JQuery?

I can use split to split some of the variables up, but I was wondering if there is an easier way to go about this, for example...

www.url.com/dir/page?setting&var1=value1

What would be the easiest way to get dir, page, and setting.

P.S. It would be nice to always select the last directory, so using a standard split won't always work if there are multiple directories.

like image 223
Devon Avatar asked Mar 25 '13 18:03

Devon


People also ask

How do you split a URL?

var newURL="http://www.example.com/index.html/homePage/aboutus/"; console. log(newURL); var splitURL=newURL. toString(). split("/"); console.


2 Answers

I'd recommend James Padolsey's URL parser -- it's a simple JS function that will give you any part of the URL (host, querystring, path, etc..)

http://james.padolsey.com/javascript/parsing-urls-with-the-dom/

like image 88
Zim Avatar answered Oct 02 '22 16:10

Zim


parseUri seems to do exactly what you're asking for, in terms of splitting the url into it's component parts.

like image 39
Bernie Avatar answered Oct 02 '22 14:10

Bernie