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