On the page http://example.com/index.html script <script src="exaple.js"></script> works

How to make this script load again when the URL changes without reloading?

For example, on http://example.com/index.html#reload .

  • example.com/index.html and example.com/index.html#reload is the same URL, because the URL does not change. Describe the essence of the problem more clearly. - DemoS
  • This is the same page but not the same URL. The address changes and I want my script to reload when the URL changes - davitp

4 answers 4

You can put a function that monitors the URL at a certain interval and calls the necessary functions when it changes.

    Well, if the address changes, then an event occurs, and the easiest way, I think, is to attach the download of the necessary scripts to this event.

      It is possible to load the js-script when changing the URL.

      • The thought is good, but I can’t write for client pages and cannot manipulate links - davitp

      m. does it make sense not to track the url, but just write handlers for these links? Mark them with the necessary id and respond to their click ( <a href="http://example.com/index.html" id="somelink">somelink</a> ). The url will always be the same, so you can send an ajax request and receive the necessary content through the id click handler. This as an option can be considered. If all the same to change the url by clicking through href, then it makes sense to look towards location.href, parse the url and perform actions. but again, you need to write a function on js, which will track clicks on the link, parse the URL and load your script (although you probably don’t load it, it’s already loaded, but run some kind of function it contains). Write more details about the situation as it should work in your understanding.