2017-06-27 28 views
-1

我使用location.relaod()重新加載當前頁面(從緩存或不)。這將重新加載頁面 - 所有GET參數都在URL中。如何重新加載沒有參數的頁面?

如果當前頁面是https://www.example.com/somewhere/page.html?a=3&b=4,是否可以觸發重新加載https://www.example.com/somewhere/page.html

目前的解決方案,我打算使用是

var fullURL = "https://www.example.com/somewhere/page.html?a=3&b=4" 
 
var splitURL = fullURL.split('?')[0] 
 
document.write(splitURL)

但也許更Javascripthic方式已經實現了?

Javascriphic從借用的Python - > Python的

+0

我很困惑 - 你的標題說'沒有它的參數',但在你說的問題中'是否有可能觸發重新加載https://www.example.com/somewhere/page.html?a=3&b= 4'包含所有的參數。你想做什麼? – Xatenev

+2

[如何獲取沒有JavaScript參數的URL?](https://stackoverflow.com/questions/6257463/how-to-get-the-url-without-any-parameters-in-javascript) – Andreas

+0

@ Xatenev:對不起,當我意識到我忘記刪除第二個URL中的參數時,我開始澄清這個問題。固定。 – WoJ

回答

2

使用location對象

// similar behavior as an HTTP redirect 
window.location.replace(location.protocol + '//' + location.host + location.pathname); 

// similar behavior as clicking on a link 
window.location.href = location.protocol + '//' + location.host + location.pathname; 
+0

與我打算使用的split()相比,這種構造是否有優勢? – WoJ

+0

可能不是。我個人不會做字符串操作,如果我不需要,因爲所有的信息已經存在。但[YMMV](https://zh.wiktionary.org/wiki/your_mileage_may_vary) – Xatenev

-3

打開控制檯,並看看document.location,你可以建立你從其中的一些領域需要的網址,如主機+路徑

+0

這不是真的即使接近有用的答案... –

+0

怎麼回事?他已經知道他必須做些什麼來改變網址,我只是告訴他他在哪裏可以找到一些已經做了他手動做的事情。分裂 –

+0

上面有更多意見的評論。答案就像下面的那些......具體的工作代碼樣本證明可以解決他最初的問題。 –

1
window.location=window.location.split("?")[0]; 

簡單地重定向..

相關問題