2011-02-02 99 views
3

是否有一些方法來替換URL與JavaScript沒有重新加載?例如window.history.replaceState()不工作,因爲我期望

www.url.com/index.php/presenter/view/../ 

www.url.com/presenter/view/../ 

?我已經與history.replaceState功能嘗試過,但它只是改變index.php後的網址..

function parseUrl(){ 
    currUrl = window.location.href; 
    verify = currUrl.indexOf("index.php"); 
    if(verify != -1){ 
     newUrl = currUrl.substring(0,verify-1); 
     newUrl += '#'+currUrl.substring(currUrl.indexOf('index.php')+10,currUrl.length); 
     if(window.history.replaceState){ 
      window.history.replaceState(null, this.title, newUrl); 
     } 
     else{ 
      window.location.href = newUrl; 
     } 
    } 

}  
+0

你能展示你的代碼嗎?因爲你只是在改變路徑,所以這應該起作用。但請注意`replaceState`不是用於替換URL,而是用於替換當前的歷史記錄。如果使用`pushState`,用戶將看到新的URL,但仍然可以使用後退按鈕返回到前一個URL(在更改之前)。 – 2011-02-02 22:11:30

+0

我需要所有鏈接ajax驅動,所以我改變window.location.hash在標準..但是例如RSS鏈接生成「正確」爲index.php/...這是想要的行爲,但我需要重定向英格利用戶到網址的散列版本..我想不做重裝(我只需要沒有這些多餘的參數,從estetic原因的URL) 我將代碼添加到我的問題.. – simekadam 2011-02-02 22:35:11

回答

4

嘗試使用History.js - 可能是跨瀏覽器對HTML5歷史API的支持將解決您的問題。

0
  1. 改變你的應用程序的文件系統位置,以便它反映你想要的網址:www.url.com/presenter/view/

  2. 使用一個index.html殼

  3. 配置您的Web服務器默認打開index.html

  4. 使用ajax抓取您的數據並將其插入到您的html文件中。

相關問題