[removed].hash refresh in Chrome?

后端 未结 3 1132
我寻月下人不归
我寻月下人不归 2021-01-18 14:32

I was doing some snooping on the web and found window.location.hash = \"etc\" to be a widely adopted method to update the browser\'s location without reloading

3条回答
  •  时光取名叫无心
    2021-01-18 15:03

    var r='#hello';
    if(navigator.userAgent.indexOf('Chrome/')!=-1){
     top.history.pushState("", "", r);
     return;
    };
    if(r.charAt(0)=='/'){
      top.location.replace(r);
     }else{
      top.location.hash=r;
    };
    

    Worked for me. And it actually took me a long time to figure this out. Firefox also supports the history object now, so we may be able to get rid of the whole "hash" thing in a few years.

    EDIT: Yes, the reloading thing is a Chrome bug.

提交回复
热议问题