Grab URL contents

Hi!

http://localhost/blah1/blah2/blah3/

Does anyone know how I can grab a portion of the above URL? I need, for example,

http://localhost/blah1/blah2/

I’m busy trying various techniques such as split(‘/’)[2]

Thank you for any help.

I’ve done this several times, but can’t find my “Reusable Code” files… thought I had them, here, but I guess that’s only at work. So!

Yeah, document.URL will get the current script, PLUS any querystring (URL parameters), so the first thing you’d want to do is split(‘?’) and use the first position [0] as the actual URL.

document.URL does include the protocol, so when you split the URL, position 0 will be http: or https:, then positions 1 will be the following //. From there, you have the FQDN (www.example.com or example.com), which will be position 2, and all the following folders, then the script, itself.

var thisURI = document.URL;
var thisURL = thisURI.split('?');
var thisURLArray = thisURL.split('/');
var thisProtocol = thisURLArray[0];
var thisFQDN = thisURLArray[2];

Etc, and so on.

HTH,

:slight_smile:

location has properties for the various parts of the URL making processing easier

folders = location.pathname.split('/');
location.protocol + location.hostname + folders[0] +'/'+ folders[1] +'/';

Hi WolfShade and felgall,

thanks for your help. I haven’t had an opportunity to try the code you posted but it looks like it should work.

Thanks again :smile: