[英]How do I log in the console the name of the file you are viewing in JavaScript?
I want to log in the console the name of the page.我想在控制台登录页面的名称。
Say you were on index.html, then in the console it would say 'index.html'假设您在 index.html 上,然后在控制台中会显示“index.html”
Get the pathname with window.location.pathname
, then find the current page by splitting it by /
s and getting the last item.使用
window.location.pathname
获取路径名,然后通过/
s 分割找到当前页面并获取最后一项。
console.log("Full URL: " + location); console.log("Current Page: " + window.location.pathname.split("/").pop());
One quick google search has given me this answer:一个快速的谷歌搜索给了我这个答案:
var url = window.location.pathname;
var filename = url.substring(url.lastIndexOf('/')+1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.