簡體   English   中英

更改URL參數而不刷新頁面

[英]Change URL parameter without refreshing the page

我一直試圖在不重新加載網站的情況下更改URL的參數,但是,我不確定100%我的代碼有什么問題。 URL中發生的事情是刪除所有參數,並將其替換為簡單的字符串。 http:// localhost:50754 / Signup555

我想從http:// localhost:50754 / Signup?A = 1&B = 2&C = 3更改為http:// localhost:50754 / Signup?A = 1&B = 2&C = 6

  function changeQueryString(searchString, documentTitle) {
        documentTitle = typeof documentTitle !== 'Pid' ? documentTitle : document.title;
        var urlSplit = (window.location.href).split("?");
        var obj = {
            Title: documentTitle,
            Url: urlSplit[0] + searchString
        };
        history.pushState(obj, obj.Title, obj.Url);
    }

<a onclick="changeQueryString('555', 'Pid')"> change it</a>

提前致謝。

您可以使用“路由器”庫。 Navigo是一個很好的例子。

您的功能應為:

  function changeQueryString(searchString, documentTitle) {
     window.history.replaceState(null, null, window.location.pathname+"?A="+searchString[0]+"&B="+searchString[1]+"&C="+searchString[2]);
    }

<a onclick="changeQueryString('555', 'Pid')"> change it</a>

暫無
暫無

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

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