且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在JavaScript中提取当前文档路径的URL的文件名?

更新时间:2023-01-28 13:03:26

如果你正在寻找路径中的最后一项,试试这个:

If you're looking for the last item in the path, try this:

var current_path = window.location.pathname.split('/').pop();

这个:

window.location.pathname

会给你类似的东西:

"/questions/6543242/how-to-extract-the-filename-of-url-in-javascript"

然后 .split()会将字符串拆分为数组,并且 .pop()将为您提供数组中的最后一项。

Then the .split() will split the string into an Array, and .pop() will give you the last item in the Array.