从路径中提取URL的目录,而不使用诸如"index.html"之类的文件名. [英] Extract URL's directory from path, without file name such as "index.html"

查看:88
本文介绍了从路径中提取URL的目录,而不使用诸如"index.html"之类的文件名.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找普通的JavaScript代码(没有jQuery等)来学习当前加载页面的URL的目录路径部分.

I am looking for plain JavaScript code (no jQuery etc.) to learn the directory path component of my currently loaded page's URL.

例如,如果我的页面加载为" http://localhost/myapp/index.html ",我想以" http://localhost/myapp/结尾.

For instance, if my page is loaded as "http://localhost/myapp/index.html", I like to end up with "http://localhost/myapp/".

我需要这样做,以便在同一位置创建指向其他文件的路径,例如到" http://localhost/myapp/other.html ".

I need this in order to create the path to a different file in the same location, e.g. to "http://localhost/myapp/other.html".

推荐答案

看起来像这样:

var href = window.location.href;
var dir = href.substring(0, href.lastIndexOf('/')) + "/";

这是一种安全的方法,还是会因为使用更复杂的URL而失败?

Is this a safe method or can this fail with more complex URLs?

这篇关于从路径中提取URL的目录,而不使用诸如"index.html"之类的文件名.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆