2 回答

TA貢獻1847條經驗 獲得超11個贊
你說你想在javascript中做到這一點,所以我假設頁面本身正在構建/修改一個鏈接,要么放在頁面上,要么直接通過javascript轉到。
在瀏覽器中的javascript中有URL對象,它可以構建和分解URL
let thisPage = new URL(window.location.href);let thatPage = new URL("https://that.example.com/path/page");
在任何情況下,一旦您有了一個URL對象,您就可以訪問它的各個部分來讀取和設置值。
添加查詢參數使用 URL 的 searchParams 屬性,您可以在其中添加參數 - 并且不必擔心管理和...該方法為您處理。?
&
thisPage.searchParams.append('yourKey', 'someValue');
這演示了它在此頁面上,添加搜索參數并在每個步驟中顯示URL:
let here = new URL(window.location.href);
console.log(here);
here.searchParams.append('firstKey', 'theValue');
console.log(here);
here.searchParams.append('key2', 'another');
console.log(here);

TA貢獻2019條經驗 獲得超9個贊
我以最簡單的方式解決了這個問題。它讓我想到,我可以通過將搜索參數添加到URL來鏈接到它。以下是我所做的:view.html
在我鏈接到的位置上,我創建了函數。我將參數添加到URL href的末尾。index.htmlview.htmlopenViewer();
function openViewer() {
window.location.href = `view.html?id={docId}`;
}
然后,我得到了這樣的參數:view.htmlURLSearchParameters
const thisPage = new URL(window.location.href);
var id = thisPage.searchParams.get('id');
console.log(id)
該頁面的新網址現在是“www.mysite.com/view.html?id=mydocid”。
添加回答
舉報