2011-02-02 441 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文件中。

相关问题