What are the differences between history.pushState & location.hash?

前端 未结 8 1395
既然无缘
既然无缘 2020-11-30 03:29

I\'m trying to update the URL using window.location.hash or history.pushState.

What are the differences and advantages of each method?

8条回答
  •  野性不改
    2020-11-30 03:59

    history.pushState is better than location.hash. but it is a HTML5 feature. so always better to have a fallback method like below.

    if (typeof(window.history.pushState) == 'function') {
        window.history.pushState(null, path, path);
    } else {
        window.location.hash = '#!' + path;
    }
    

提交回复
热议问题