How to get application path using javascript

If I have an application hosted in a directory. The application path is the name of the directory.

for example

http://192.168.1.2/AppName/some.html

How to get application name using javascript, as in my case /AppName .

document.domain returns the host name, and document.URL returns the entire URL.

EDIT

The application path may be more complex, for example /one/two/thre/

+7
javascript
source share
5 answers

This will give you the result, but it will need to be changed if you have more levels (see comment).

 var path = location.pathname.split('/'); if (path[path.length-1].indexOf('.html')>-1) { path.length = path.length - 1; } var app = path[path.length-2]; // if you just want 'three' // var app = path.join('/'); // if you want the whole thing like '/one/two/three' console.log(app); 
+8
source share
 window.location.pathname.substr(0, window.location.pathname.lastIndexOf('/')) 
+9
source share
 (function(p) { var s = p.split("/").reverse(); s.splice(0, 1); return s.reverse().join("/"); })(location.pathname) 

This expression ... just copy it to the place where this line is needed. Or put it in a variable so you can use it several times.

+2
source share

That should do the trick

 function getAppPath() { var pathArray = location.pathname.split('/'); var appPath = "/"; for(var i=1; i<pathArray.length-1; i++) { appPath += pathArray[i] + "/"; } return appPath; } alert(getAppPath()); 
+2
source share

alert("/"+location.pathname.split('/')[1]);

If your path is like / myApp / ...

or

 function applicationContextPath() { if (location.pathname.split('/').length > 1) return "/" + location.pathname.split('/')[1]; else return "/"; } alert(applicationContextPath); 
0
source share

All Articles