I'm trying to find a relatively easy and reliable method to extract the base URL from a string variable using JavaScript (or jQuery).
For example, given something like:
http://www.sitename.com/article/2009/09/14/this-is-an-article/
I'd like to get:
http://www.sitename.com/
Is a regular expression the best bet? If so, what statement could I use to assign the base URL extracted from a given string to a new variable?
I've done some searching on this, but everything I find in the JavaScript world seems to revolve around gathering this information from the actual document URL using location.host or similar.
A lightway but complete approach to getting basic values from a string representation of an URL is Douglas Crockford's regexp rule:
If you are looking for a more powerful URL manipulation toolkit try URI.js It supports getters, setter, url normalization etc. all with a nice chainable api.
If you are looking for a jQuery Plugin, then jquery.url.js should help you
A simpler way to do it is by using an anchor element, as @epascarello suggested. This has the disadvantage that you have to create a DOM Element. However this can be cached in a closure and reused for multiple urls:
Use it like so:
If you are extracting information from window.location.href (the address bar), then use this code to get
http://www.sitename.com/
:If you have a string,
str
, that is an arbitrary URL (not window.location.href), then use regular expressions:I, like everyone in the Universe, hate reading regular expressions, so I'll break it down in English:
No need to create DOM elements or do anything crazy.
There is no reason to do splits to get the path, hostname, etc from a string that is a link. You just need to use a link
You can easily do it with jQuery appending the element and reading its attr.
Instead of having to account for window.location.protocol and window.location.origin, and possibly missing a specified port number, etc., just grab everything up to the 3rd "/":
If you're using jQuery, this is a kinda cool way to manipulate elements in javascript without adding them to the DOM: