Get base url and file path from my windows url using Javascript

Hi I tried to get the base url and file path from my windows url. But I can't get it .. Please correct me.

Url:

http://sample.com:30023/portal/site/samples/index.jsp

Current output:  http://sample.com:30023/index.jsp ?

Desired result: http://sample.com:30023/portal/site/samples/

Used code:

var baseURL = location.protocol + "//" + location.hostname + (location.port && ":" + location.port) + "/";

      

+3


source to share


2 answers


add location.pathname so it becomes

var baseURL = location.protocol + "//" + location.hostname + (location.port && ":" + location.port) + location.pathname;

      



Also, why not just use location.href to get it all?

Good link at https://developer.mozilla.org/en-US/docs/DOM/window.location

+5


source


Perhaps something like this:



var sBase = location.href.substr(0, location.href.lastIndexOf("/") + 1);

      

+2


source







All Articles