jQuery 如何通过JQuery从没有扩展名的URL中获取页面名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16286384/
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
How to get the pagename from the URL without the extension through JQuery
提问by Steve
I have a URL:-
我有一个网址:-
http://www.example.com/keyword/category.php
or
或者
http://www.example.com/keyword/category.php#4
I need a magic abracadabra which gives me only the pagename as categoryfrom this URL.
我需要一个神奇的 abracadabra,它只给我这个 URL 中的页面名称作为类别。
Here is what I tried, and it gives category.php. But it has two problems. It is ugly and long and it gives me filename with an extension.
这是我尝试过的,它提供了category.php。但它有两个问题。它又丑又长,它给了我带扩展名的文件名。
var currurl = window.location.pathname;
var index = currurl.lastIndexOf("/") + 1;
var filename = currurl.substr(index);
Thanks.
谢谢。
回答by nikeaa
Just make this into a function as below:
只需将其变成如下函数:
function getPageName(url) {
var index = url.lastIndexOf("/") + 1;
var filenameWithExtension = url.substr(index);
var filename = filenameWithExtension.split(".")[0]; // <-- added this line
return filename; // <-- added this line
}
Then when you need to use it:
然后当你需要使用它时:
var url = "http://www.example.com/keyword/category.php";
var myFilename = getPageName(url);
All of the "ugliness" has been hidden in a function and the main code looks nice and clean!
所有的“丑陋”都隐藏在一个函数中,主代码看起来又漂亮又干净!
回答by Hadi ????
function getPageName() {
var index = window.location.href.lastIndexOf("/") + 1,
filenameWithExtension = window.location.href.substr(index),
filename = filenameWithExtension.split(".")[0];
return filename;
}
回答by Drakes
Note:If you do .split('.')
, you will miss the base names of many URLs.
注意:如果这样做.split('.')
,您将错过许多 URL 的基本名称。
You can find the last forward slash and search ahead for the first .
, ?
, &
or #
to catch variations of URLs. This is probably the equivalent of PHP's basename
你可以找到最后的斜线和第一个搜索前进.
,?
, &
或#
URL来捕捉变化。这可能相当于 PHP 的basename
function getBaseName(url) {
if(!url || (url && url.length === 0)) {
return "";
}
var index = url.lastIndexOf("/") + 1;
var filenameWithExtension = url.substr(index);
var basename = filenameWithExtension.split(/[.?&#]+/)[0];
// Handle '/mypage/' type paths
if(basename.length === 0) {
url = url.substr(0,index-1);
basename = getBaseName(url);
}
return basename ? basename : "";
}
and use it like so
并像这样使用它
var url = "http://www.example.com/keyword/category.php#4";
var file = getBaseName(url);
Results:
结果:
http://www.example.com/keyword/category.php#4 => "category"
http://www.example.com/keyword/category => "category"
http://www.example.com/keyword/category/ => "category"
http://www.example.com/keyword/category?ver=1 => "category"
http://www.example.com/keyword/category/?ver=1 => "category"
http://www.example.com/keyword/category#elem => "category"
JSBin demo
JSBin 演示
回答by map
For work with querystring like http://www.example.com/keyword/category.php?parametro=teste
对于像http://www.example.com/keyword/category.php?parametro=teste这样的查询字符串的工作
function getPageName(url) {
var index = url.lastIndexOf("/") + 1;
var filenameWithExtension = url.substr(index);
var filename = filenameWithExtension.split(".")[0];
filename = filename.split("?")[0]; // <-- added this line
return filename;
}