For example, if I click the 'next' button on a webpage, the website displays the results of the next page but does not reload the page, whilst the URL changes. How would I detect this change in URL and trigger an event accordingly on the newly displayed page?
            Asked
            
        
        
            Active
            
        
            Viewed 877 times
        
    0
            
            
        - 
                    https://stackoverflow.com/questions/3522090/event-when-window-location-href-changes – stackzebra Jan 23 '22 at 22:04
- 
                    Thanks for this link. One of the solutions worked :) – Cem Ratip Jan 23 '22 at 23:03
1 Answers
0
            
            
        After click 'next' button you would do action which get new URL address and do action based on the new data.
For example:
nextBtn.addEventListener('click', () => {
  const currentLocation = window.location;
  // here you can get data from URL and do whatever you want with it
});
But you can also use library like rxjs to subscribe url from others files.
For example:
file1.js
nextBtn.addEventListener('click', () => {
  const currentLocation = window.location;
  publisher.next(currentLocation);
});
file2.js
publisher.subscribe(location => {
  // and you have your url data in another file
});
 
    
    
        Paweł Reliński
        
- 1
- 1
