function getPageFileName() {
var url = window.location.href;
var fileName = url.substring(url.lastIndexOf('/') + 1);
// 移除查詢字串與 hash
fileName = fileName.split('?')[0].split('#')[0];
return fileName || 'index'; // 根目錄回傳 'index'
}
console.log(getPageFileName()); // e.g. 'article.html'
alert('目前頁面:' + getPageFileName());
article.html → article.js)。location.href 不會改變,需改用路由庫提供的當前路徑。