On - [removed].hash - Change?

后端 未结 13 1383
谎友^
谎友^ 2020-11-21 13:38

I am using Ajax and hash for navigation.

Is there a way to check if the window.location.hash changed like this?

http://example.com/blah

13条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-21 13:56

    I was using this in a react application to make the URL display different parameters depending what view the user was on.

    I watched the hash parameter using

    window.addEventListener('hashchange', doSomethingWithChangeFunction());
    

    Then

    doSomethingWithChangeFunction () { 
        // Get new hash value
        let urlParam = window.location.hash;
        // Do something with new hash value
    };
    

    Worked a treat, works with forward and back browser buttons and also in browser history.

提交回复
热议问题