views:

2900

answers:

5

I'm trying to find a relatively easy and reliable method to extract the base URL from a string variable using JavaScript/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.

Thanks in advance for any guidance!

+3  A: 
pathArray = window.location.pathname.split( '/' );
host = pathArray[2];
Rafal Ziolkowski
Thanks for the reply, but again, I'm trying to extract the base URL from a string, rather than the actual document URL. I don't think this will help me - though please correct me if I'm wrong.
Bungle
pathArray = String("http://YourHost.com/url/nic/or/not").split( '/' );host = pathArray[2];
Rafal Ziolkowski
split works for strings, does not matter where are they coming from
Rafal Ziolkowski
this works but it's the first value you need, pathArray[0]
daddywoodland
Got it - thanks Rafal and daddywoodland! I ended up using: url = 'http://www.sitename.com/article/2009/09/14/this-is-an-article/'; pathArray = (url).split('/'); host = 'http://' + pathArray[2];I think Rafal's example just omitted the "http://" that is present in all of the strings that I'm processing, in which case the pathArray[2] is the one you need. Without the "http://" prefix, pathArray[0] would be the one. Thanks again.
Bungle
Ahhh, I see what happened. Rafal's example was indeed correct, but the "http://" prefix meant that it was interpreted as a link in his comment, so it was truncated from the visible text.
Bungle
Yep, It depends if You have http or not. You can always run one more test if first array elm is http or not :)
Rafal Ziolkowski
Why all the variable declaration? `url = 'sitename.com/article/2009/09/14/this-is-an-article'; newurl = 'http://' + url.split('/')[0];`
Emtucifor
+1  A: 

Don't need to use jQuery, just use

window.location.hostname
daddywoodland
Thanks - I can't use that with a string, though, can I? My understanding is that will only work with the document URL.
Bungle
oh yeah, Rafal Ziolkowski answered it then but you want pathArray[0] not 2...
daddywoodland
A: 

You can do it using a regex :

/(http:\/\/)?(www)[^\/]+\//i

does it fit ?

Clement Herreman
Hmm, from my limited regex skills, it looks like that's at least close. I'll add some more information to the question to see if I can help narrow down the best regex.
Bungle
I ended up using .split('/') on the string just because it was an easier solution for me. Thanks for your help, though!
Bungle
https URLs? Host names not starting with www? Why capture the www anyway?
Tim Down
Clement Herreman
+1  A: 

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

//create a new element link with your link
var a = document.createElement("a");
a.href="http://www.sitename.com/article/2009/09/14/this-is-an-article/";

//hide it from view when it is added
a.style.display="none";

//add it
document.body.appendChild(a);

//read the links "features"
alert(a.protocol);
alert(a.hostname)
alert(a.pathname)
alert(a.port);
alert(a.hash);

//remove it
document.body.removeChild(a);

You can easily do it with jQuery appending the element and reading its attr.

epascarello
Why add 50K of jQuery when you've shown how to do it without jQuery in a few bytes?
Tim Down
Because the poster says they are using jQuery.
epascarello
Ah yes, fair enough. Though when it's as simple as this I see no value in using the extra layer of abstraction that using jQuery would add.
Tim Down
A: 

If you're using jQuery, this is a kinda cool way to manipulate elements in javascript without adding them to the DOM:

var myAnchor = $("<a />");

//set href    
myAnchor.attr('href', 'http://example.com/path/to/myfile')

//your link's features
var hostname = myAnchor.attr('hostname'); // http://example.com
var pathname = myAnchor.attr('pathname'); // /path/to/my/file
//...etc
Wayne