views:

37

answers:

4

I am looking to write a piece of javascript that will do the following:

Look at current url and return any folders in the url ie:

http://localhost/folder1/page.aspx returns -> /folder1/

http://localhost/page.aspx returns -> /

Any help?

A: 

Via window.location you can access the full URL of your running script. Then you can use a REGEX to extract the part you want, in this case the path.

joni
+1  A: 

You can try window.location.pathname to get its path. for ref

Chinmayee
A: 

The location property (on window) (link) has a variety of properties that you can use to get that information.

Here's an example of what's available:

var loc, name, value;

loc = window.location;
for (name in window.location) {
  value = loc[name];
  if (typeof value != 'function') {
    display(name + ": " + value);
  }
}

function display(msg) {
  var p = document.createElement('p');
  p.innerHTML = msg;
  document.body.appendChild(p);
}
T.J. Crowder
A: 
window.location.pathname.substr(0, window.location.pathname.lastIndexOf("/") + 1);
David Mårtensson