最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - window.history.replaceState() not working as I expect - Stack Overflow

programmeradmin1浏览0评论

is there some way to replace url with JavaScript without reload? For example

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

to

www.url/presenter/view/../ 

? I've tried it with history.replaceState function but it is only changing the url after 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;
        }
    }

}    

is there some way to replace url with JavaScript without reload? For example

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

to

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

? I've tried it with history.replaceState function but it is only changing the url after 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;
        }
    }

}    
Share Improve this question edited Feb 2, 2011 at 22:36 simekadam asked Feb 2, 2011 at 22:04 simekadamsimekadam 7,38411 gold badges59 silver badges80 bronze badges 4
  • Could you show your code? As you are only changing the path, this should work. But note that replaceState is not for replacing the URL but for replacing the current history entry. If you use pushState, the user will see new URL, but will still be able to use the back button to return to the previous URL (before you changed it). – Felix Kling Commented Feb 2, 2011 at 22:11
  • I need have all links ajax driven so I am changing the window.location.hash at standard..But for example RSS links are generated "right" as index.php/... That is wanted behaviour but I need redirect inig users to the hashed version of url..And I wanted to do it without reload(I only need to have the URL without these superfluous parameters from estetic reasons) I added the code to my question.. – simekadam Commented Feb 2, 2011 at 22:35
  • How does newUrl look like after you made all the modifications? – Felix Kling Commented Feb 2, 2011 at 22:42
  • OH..it is working now..apparently it seems to be:DI really dont know I let my code wait for later and now..Pretty nice..But you may refer to there is bad increment (+10 instead +11)..that was my mistake..So thanks and I will now pray for less IE positive visitors of my site – simekadam Commented Feb 2, 2011 at 23:42
Add a ment  | 

3 Answers 3

Reset to default 4

Try using History.js - perhaps it's cross-browser support for the HTML5 History APIs will solve your issue.

The HTML5 History API can do this, but isn't supported by most browsers (only FF4, and Chrome AFAIK)

GitHub are using it for their Tree Slider.

  1. change the file system location of your application so that it reflects the url you want: www.url./presenter/view/

  2. use an index.html shell

  3. configure your web server to open index.html by default

  4. use ajax to grab your data and insert it into your html file.

发布评论

评论列表(0)

  1. 暂无评论