开发者

return folders from url

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 -> /fol开发者_Go百科der1/

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

Any help?


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


window.location.pathname.substr(0, window.location.pathname.lastIndexOf("/") + 1);


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.


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜