Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How-to get the Application path using javascript

Tags:

javascript

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

For example

http://192.168.1.2/AppName/some.html

How to get using javascript the application name like in my case /AppName.

document.domain is returning the hostname and document.URL is returning the whole Url.

EDIT

Thr app path could be more complex, like /one/two/thre/

like image 905
user137348 Avatar asked Sep 09 '10 14:09

user137348


Video Answer


2 Answers

window.location.pathname.substr(0, window.location.pathname.lastIndexOf('/'))
like image 59
electron Avatar answered Sep 20 '22 05:09

electron


This will give you a result but would have to be modified if you have more levels (see commented code).

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);
like image 29
donohoe Avatar answered Sep 19 '22 05:09

donohoe