Javascript jQuery/JS:获取当前URL父目录
发件人: 我怎样才能得到: 并使用Javascript将其存储到变量中,以及如何使用jQuery。提前感谢。Javascript jQuery/JS:获取当前URL父目录,javascript,jquery,url,Javascript,Jquery,Url,发件人: 我怎样才能得到: 并使用Javascript将其存储到变量中,以及如何使用jQuery。提前感谢。 var a = "http://www.site.com/example/index.html"; var b = a.substring(0, a.lastIndexOf('/'))+"/"; 正则表达式也可以这样做,但在本例中,正则表达式并不是最简单的解决方案 var url = "http://www.site.com/example/index.html"; var new
var a = "http://www.site.com/example/index.html";
var b = a.substring(0, a.lastIndexOf('/'))+"/";
正则表达式也可以这样做,但在本例中,正则表达式并不是最简单的解决方案
var url = "http://www.site.com/example/index.html";
var newUrl = url.match(/^(.*[\\\/])/)[1];
当前页面的演示过程:
注释和参考资料:
- :location对象包含有关当前URL的信息
- :当前页面的整个URL
- :获取元素的属性值
- :该方法用于将字符串拆分为子字符串数组,并返回新数组
- :该方法将数组中的选定元素作为新数组对象返回
- :该方法将数组的元素合并为字符串,并返回字符串
- 以下方法似乎有效
新URL(“.”,“http://example.com/folder/subfolder/file.js")
小挑剔。。。那不是“目录”。它是资源URL的一部分。它可能映射到服务器上的一个目录,但URL本身没有目录,因为HTTP不是文件系统。感谢您提供的信息,非常感谢您是否介意扩展您的答案,以供其他程序员理解,它到底如何帮助解决问题。这是一个更好的答案,因为您可以很容易地找到祖父母,等
var url = "http://www.site.com/example/index.html";
var newUrl = url.match(/^(.*[\\\/])/)[1];
var s1 = "http://www.site.com/example/index.html";
var s2 = s1.replace(s1.split("/").pop(),"");
$(location).prop("href").split("/").slice(0,-1).join("/")
{
"ancestorOrigins": {
},
"hash": "",
"host": "stackoverflow.com",
"hostname": "stackoverflow.com",
"href": "https://stackoverflow.com/questions/17497045/jquery-js-get-current-url-parent-directory",
"origin": "https://stackoverflow.com",
"pathname": "/questions/17497045/jquery-js-get-current-url-parent-directory",
"port": "",
"protocol": "https:",
"search": ""
}
https://stackoverflow.com/questions/17497045/jquery-js-get-current-url-parent-directory
[
"https:",
"",
"stackoverflow.com",
"questions",
"17497045",
"jquery-js-get-current-url-parent-directory"
]
[
"https:",
"",
"stackoverflow.com",
"questions",
"17497045"
]
https://stackoverflow.com/questions/17497045