2

I believe you know that some sites have built on "one-page system".

In such systems after clicking on the link the site do not moves user to another page but just reloads the page with Ajax and changes the URL.

I saw many answers about $(window).on("hashchange") and I know this trick, but I need to detect the WHOLE URL change (NOT just hash of it) – so I need something else.

Any ideas how to detect the whole URL change?

Kir Mazur
  • 997
  • 2
  • 13
  • 27
  • 1
    Wouldn't a change to the url other than the hash cause the page to reload? Thus *NOT* a SPA? – Taplar Dec 06 '16 at 20:34
  • I think you should consider using [$.unload](https://api.jquery.com/unload/) as an alternative option – leo.fcx Dec 06 '16 at 20:36
  • $.unload doesn't happens because the page actually doesn't "unloading" – only URL is changing. For example when I click on Play button on https://www.netflix.com/title/70178217 the page do not reloads - it just changes the url to https://www.netflix.com/watch/80049213 – Kir Mazur Dec 06 '16 at 20:40
  • @Taplar nope. window.history.pushState() in html5 let you to change the URL without reloading the page. – Kir Mazur Dec 06 '16 at 20:42
  • Interesting. Well, I'm unaware of any events that are generated by the use of pushState. – Taplar Dec 06 '16 at 20:47
  • This is what you are looking for https://stackoverflow.com/a/20544601/1362713 – M_R_K Dec 28 '17 at 23:49

2 Answers2

2

I didn't find how to detect the whole URL change but this question learned me a few new things.

  1. You can detect with jQuery .on() user click on the link that cases "page reload emulation" and use such event as "page reload event".
  2. Now I know that function window.history.pushState() in html5 let you to change the URL without reloading the page.

Hope, it will help somebody.

Kir Mazur
  • 997
  • 2
  • 13
  • 27
0

I know it's an old one, but I guess this can come handy to anybody who stumbles upon this post. The way is indeed the history API:

window.onpopstate = history.onpushstate = function(e) {//yourcode}

This will detect any change in the url of the page, very handy.

More info on history API here

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Marghen
  • 255
  • 2
  • 6