2013-12-10 108 views
0

我已經看到幾個關於通過JavaScript重定向到頁面的問題,這很好,但他們都只是重定向到一個基本的URL。我想包含一個可變路徑。所以說例如我有http://www.glas.com/my/url/path我想用window.location.replace()來替換glas部分只,並將其更改爲glass。我是JavaScript的初學者,所以任何幫助將不勝感激。重定向URL包含的路徑

這是我到目前爲止已經試過:

var path = ''; // this is where I'm stuck 
window.location.replace("http://www.glass.com/" + path); 

path變量將解析當前URL爲/後什麼。我曾試着查看JavaScript的matchregexp,但它似乎都很混亂。

回答

1

嘗試

window.location = window.location.href.replace('glas', 'glass'); 
+0

*臉掌*哇。簡單得簡單。謝謝。 :) –

1

你會做這樣的事情:

window.location.href = window.location.href.replace('glas','glass'); 
+0

啊。謝謝。 :) –