How can I detect an address bar change with JavaScript?

后端 未结 4 1826

I have a Ajax heavy application that may have a URL such as

http://example.com/myApp/#page=1

When a user manipulates the site, the address ba

4条回答
  •  半阙折子戏
    2020-11-27 15:53

    check the current address periodically using setTimeout/interval:

     var oldLocation = location.href;
     setInterval(function() {
          if(location.href != oldLocation) {
               // do your action
               oldLocation = location.href
          }
      }, 1000); // check every second
    

提交回复
热议问题