簡體   English   中英

SPA 訪問新頁面時如何刪除 URL 中的#?

[英]SPA How to remove the # in the URL when visiting new page?

我正在使用 Django 和 vanilla JavaScript,在我的本地主機上,當我單擊一個按鈕以顯示一個新頁面時,我的 URL 轉到本地主機:

如何不顯示#?

我正在嘗試使用 history.pushState(null, ' ', "all_posts"); 將 URL 添加到頁面中; 例如,現在我的 URL 顯示

http://localhost:8000/all_posts#

 const url = new URL('http://localhost:8000/all_posts#') url.hash = ''; console.log(url)

您可以在頁面加載后使用正則表達式替換window.location.href中不必要的字符,然后更新 URL。

let presentURL = window.location.href;

if  ( presentURL.endsWith('#') ) {
  presentURL.replace(/#(\S)/g, '$1');
  window.location.href = presentURL;
} // IF

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM