i have an html page and i would like inside the html page to retrieve the name of the html document via Javascript. Is that possible?
我有一个HTML页面,我想在html页面内通过Javascript检索html文档的名称。那可能吗?
e.g. name of html document = "indexOLD.html"
例如html document的名称=“indexOLD.html”
7 个解决方案
#1
81
var path = window.location.pathname;
var page = path.split("/").pop();
console.log( page );
#2
22
Current page: It's possible to do even shorter. This single line sound more elegant to find the current page's file name:
当前页面:可以做得更短。这条单线声音更加优雅,可以找到当前页面的文件名:
var fileName = location.href.split("/").slice(-1);
or...
要么...
var fileName = 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; }
#3
7
Try this
尝试这个
location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
location.pathname gives the part(domain not included) of the page url. To get only the filename you have to extaract it using substring method.
location.pathname给出页面url的部分(不包括域)。要只获取文件名,您必须使用substring方法来提取它。
#4
4
Use: location.pathname
使用:location.pathname
alert(location.pathname);
http://jsfiddle.net/yQqe3/
https://developer.mozilla.org/en-US/docs/DOM/window.location
https://developer.mozilla.org/en-US/docs/DOM/window.location
#5
3
Use window.location.pathname
to get the path of the current page's URL.
使用window.location.pathname获取当前页面URL的路径。
#6
2
This will work even if the url ends with a /
:
即使网址以/结尾,这也会有效:
var segments = window.location.pathname.split('/');
var toDelete = [];
for (var i = 0; i < segments.length; i++) {
if (segments[i].length < 1) {
toDelete.push(i);
}
}
for (var i = 0; i < toDelete.length; i++) {
segments.splice(i, 1);
}
var filename = segments[segments.length - 1];
console.log(filename);
#7
0
Single statement that works with trailing slash. If you are using IE11 you'll have to polyfill the filter
function.
使用尾部斜杠的单个语句。如果您使用的是IE11,则必须填充过滤器功能。
var name = window.location.pathname
.split("/")
.filter(function (c) { return c.length;})
.pop();
#1
81
var path = window.location.pathname;
var page = path.split("/").pop();
console.log( page );
#2
22
Current page: It's possible to do even shorter. This single line sound more elegant to find the current page's file name:
当前页面:可以做得更短。这条单线声音更加优雅,可以找到当前页面的文件名:
var fileName = location.href.split("/").slice(-1);
or...
要么...
var fileName = 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; }
#3
7
Try this
尝试这个
location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
location.pathname gives the part(domain not included) of the page url. To get only the filename you have to extaract it using substring method.
location.pathname给出页面url的部分(不包括域)。要只获取文件名,您必须使用substring方法来提取它。
#4
4
Use: location.pathname
使用:location.pathname
alert(location.pathname);
http://jsfiddle.net/yQqe3/
https://developer.mozilla.org/en-US/docs/DOM/window.location
https://developer.mozilla.org/en-US/docs/DOM/window.location
#5
3
Use window.location.pathname
to get the path of the current page's URL.
使用window.location.pathname获取当前页面URL的路径。
#6
2
This will work even if the url ends with a /
:
即使网址以/结尾,这也会有效:
var segments = window.location.pathname.split('/');
var toDelete = [];
for (var i = 0; i < segments.length; i++) {
if (segments[i].length < 1) {
toDelete.push(i);
}
}
for (var i = 0; i < toDelete.length; i++) {
segments.splice(i, 1);
}
var filename = segments[segments.length - 1];
console.log(filename);
#7
0
Single statement that works with trailing slash. If you are using IE11 you'll have to polyfill the filter
function.
使用尾部斜杠的单个语句。如果您使用的是IE11,则必须填充过滤器功能。
var name = window.location.pathname
.split("/")
.filter(function (c) { return c.length;})
.pop();