如何在 Javascript 中获取 html 页面的名称?

新手上路,请多包涵

我有一个 html 页面,我想在 html 页面内通过 Javascript 检索 html 文档的名称。那可能吗?

例如 html 的名称 document = "indexOLD.html"

原文由 programmer 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 506
2 个回答
var path = window.location.pathname;
var page = path.split("/").pop();
console.log( page );

原文由 Daniel Aranda 发布,翻译遵循 CC BY-SA 3.0 许可协议

当前页面:可以做得更短。这条单行查找当前页面的文件名听起来更优雅:

 var fileName = location.href.split("/").slice(-1);

或者…

 var fileName = location.pathname.split("/").slice(-1)

自定义导航框的链接很酷,因此指向当前的链接由 CSS 类启发。

记者:

 $('.menu a').each(function() {
    if ($(this).attr('href') == location.href.split("/").slice(-1)){ $(this).addClass('curent_page'); }
});

CSS:

 a.current_page { font-size: 2em; color: red; }

原文由 Hugolpz 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题