Javascript 从地址栏获取页面文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8497050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the page file name from the address bar
提问by Boardy
I was wondering if it would be possible to get the page name from the address bar using jquery or javascript. I know this can be done using PHP but don't really want to as it is only a html website.
我想知道是否可以使用 jquery 或 javascript 从地址栏中获取页面名称。我知道这可以使用 PHP 来完成,但并不真正想要,因为它只是一个 html 网站。
I.e. if the address is www.mywebsite.com/hello.htmhow do I get the hello.htmpart out of the address.
即如果地址是www.mywebsite.com/hello.htm我如何hello.htm从地址中取出部分。
Thanks for any help you can provide.
感谢您的任何帮助,您可以提供。
回答by ShankarSangoli
Try this
尝试这个
location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
location.pathnamegives the part(domain not included) of the page url. To get only the filename you have to extaract it using substringmethod.
location.pathname给出页面 url 的部分(不包括域)。要仅获取文件名,您必须使用substring方法提取它。
回答by Juan Mendes
https://developer.mozilla.org/en/DOM/window.location
https://developer.mozilla.org/en/DOM/window.location
alert(location.pathname)
If you don't want the leading slash, you can strip it out.
如果你不想要前导斜杠,你可以把它去掉。
location.pathname.substring(1)
回答by Hugolpz
Current page: The single line sound more elegant to find the current page's file name:
当前页面:单行听起来更优雅,以查找当前页面的文件名:
location.href.split("/").slice(-1)
or
或者
location.pathname.split("/").slice(-1)
This is cool to customize nav box's link, so the link toward the current is enlighten by a CSS class.
自定义导航框的链接很酷,因此指向当前的链接由 CSS 类启发。
JS:
JS:
$('.menu a').each(function() {
if ($(this).attr('href') == location.href.split("/").slice(-1)){ $(this).addClass('curent_page'); }
});
CSS:
CSS:
a.current_page { font-size: 2em; color: red; }
回答by austincheney
Try this:
尝试这个:
var pageName = (function () {
var a = window.location.href,
b = a.lastIndexOf("/");
return a.substr(b + 1);
}());
回答by Phil Klein
The Location object is your friend:
Location 对象是你的朋友:
var pageName = location.pathname.substring(1);
回答by Thielicious
document.URL.match(/[^\/]+$/);
document.URL.match(/[^\/]+$/);
Just a simple alternative.
只是一个简单的选择。
document.URLreturns the document's location.
.match()is a method that filters a string using Regular Expression./[^\/]+$/fetches the rest of the string starting after the last occurring slash /.
document.URL返回文档的位置。
.match()是一种使用正则表达式过滤字符串的方法。/[^\/]+$/从最后出现的斜杠开始获取字符串的其余部分/。
回答by Remus
I've had an issue where i needed to remove the querystring parameters (?) and/or anchor (#) tags. I did this:
我遇到了一个问题,需要删除查询字符串参数 (?) 和/或锚 (#) 标签。我这样做了:
var path = window.location.pathname.toLowerCase();
// find the end of path prior the Query and Anchor designations. strip off everything after the ? or #.
// if the ? is first, then use ?, else use the #, else use the string length.
// then replace all \ with /
// then split it into an array based on /
var pagePathAry = path.substr(0, path.indexOf("?") > -1 && path.indexOf("?") < path.indexOf("#") ? path.indexOf("?") : path.indexOf("#") > -1 ? path.indexOf("#") : path.length).replace("\", "/").split("/");
// get the folder it's in
var subFolder = pagePathAry[pagePathAry.length - 2];
// get the page name
var pageName = pagePathAry[pagePathAry.length - 1];

