繁体   English   中英

如何在控制台登录您在 JavaScript 中查看的文件的名称?

[英]How do I log in the console the name of the file you are viewing in JavaScript?

我想在控制台登录页面的名称。

假设您在 index.html 上,然后在控制台中会显示“index.html”

使用window.location.pathname获取路径名,然后通过/ s 分割找到当前页面并获取最后一项。

 console.log("Full URL: " + location); console.log("Current Page: " + window.location.pathname.split("/").pop());

一个快速的谷歌搜索给了我这个答案:

var url = window.location.pathname;
var filename = url.substring(url.lastIndexOf('/')+1);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM