来源:
如何仅获取:
使用JavaScript将其存储在变量中,并使用jQuery也是如此。提前致谢。
来源:
如何仅获取:
使用JavaScript将其存储在变量中,并使用jQuery也是如此。提前致谢。
var myURL = "http://www.site.com/example/index.html";
var myDir = myURL.substring( 0, myURL.lastIndexOf( "/" ) + 1);
$(location).prop("href").split("/").slice(0,-1).join("/")
当前页面的演示过程:
$(location)
{
"ancestorOrigins": {
},
"hash": "",
"host": "stackoverflow.com",
"hostname": "stackoverflow.com",
"href": "https://dev59.com/PmMm5IYBdhLWcg3wlv0K",
"origin": "https://stackoverflow.com",
"pathname": "/questions/17497045/jquery-js-get-current-url-parent-directory",
"port": "",
"protocol": "https:",
"search": ""
}
$(location).prop("href")
https://dev59.com/PmMm5IYBdhLWcg3wlv0K
$(location).prop("href").split("/")
[
"https:",
"",
"stackoverflow.com",
"questions",
"17497045",
"jquery-js-get-current-url-parent-directory"
]
$(location).prop("href").split("/").slice(0,-1)
[
"https:",
"",
"stackoverflow.com",
"questions",
"17497045"
]
※ The slice() method selects the elements starting at the given start argument, and ends at, but does not include, the given end argument. Use negative numbers to select from the end of an array.
$(location).prop("href").split("/").slice(0,-1).join("/")
https://dev59.com/PmMm5IYBdhLWcg3wlv0K
注释和参考资料:
var s1 = "http://www.site.com/example/index.html";
var s2 = s1.replace(s1.split("/").pop(),"");
new URL(".", "http://example.com/folder/subfolder/file.js")
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];