I finally managed to reach a solution which fits my case. I added the following line of code to my .js file.
This way, everytime the URL of the page changes, the function
is called. This event is only triggered after the page's DOM is completely loaded.