fix(script): 修复从其他页面切入时,关于页无法正确获取主机名的问题

This commit is contained in:
Ad-closeNN
2025-09-19 23:08:19 +08:00
parent e97de007a8
commit 3945ee08d4
+8
View File
@@ -115,6 +115,7 @@ the error log is like driving with your eyes closed.
- <a href="/assets/carbon-config.json" target="_blank">/public/assets/carbon-config.json</a> [Carbon 官网](https://carbon.now.sh) 配置文件。 - <a href="/assets/carbon-config.json" target="_blank">/public/assets/carbon-config.json</a> [Carbon 官网](https://carbon.now.sh) 配置文件。
<script> <script>
function getHostname(){
// 主机名解析 // 主机名解析
const hostname = window.location.hostname; const hostname = window.location.hostname;
const siteType = document.getElementById('cdns'); const siteType = document.getElementById('cdns');
@@ -181,4 +182,11 @@ the error log is like driving with your eyes closed.
else { else {
hName.textContent = "本地 HTML 文件"; hName.textContent = "本地 HTML 文件";
} }
}
// 兼容 Astro 的客户端导航与首次加载
document.addEventListener('astro:page-load', getHostname);
document.addEventListener('astro:after-swap', getHostname);
if (document.readyState !== 'loading') getHostname();
document.addEventListener('DOMContentLoaded', getHostname);
</script> </script>