简体   繁体   English

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

[英]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.

相关问题 如何从不受信任的Javascript登录到错误控制台? - How do you log to the error console from untrusted Javascript? 如何在JavaScript文件console.log中添加Rails控制器变量? - How do I add a rails controller variable to javascript file console.log? 我如何将用户名记录到控制台? - How do i log the user's name to console? 如何创建格式化的 javascript 控制台日志消息 - How do I create formatted javascript console log messages 如何将数据从控制台日志传输到 json 文件? - How do I transfer data from console log to a json file? 如何在不进入 JavaScript 的新行的情况下将内容记录到控制台? - How do you log something to the console without going to a new line in JavaScript? 在网页中没有 [“{ and the }”] 的情况下,如何在 JavaScript console.log() 中打印出 [{ and }] ? - How do you print out [{ and }] in JavaScript console.log() without the [“{ and the }”] in web page? 如何使用点表示法在JavaScript中向数组和对象原型添加console.log方法? - How do you add a console.log method on to arrays and objects prototypes in JavaScript using the dot notation? 如何在Winston中将错误对象记录到文件而不是控制台? - How do you log an error object to file rather than console in Winston? 你如何从 JavaScript 的控制台 output 中去掉“name”和“count”的双引号? - How do you get rid of double quotes from "name" and "count" from the console output in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM