0

I have a problem because I need to detect when my URL change and I don't find the solution. I have seen "window.onhashchange" but that is only for URL that contain "#" symbol. In my case, I dont have in my URL the Symbol "#" so for example, if my URL is "/myURL/oneOption" and change to "/myURL/secondOption" I need to detect that. Somebody know how can I to do that?

Im developping a Java application and Im using HTML, CSS and Javascript (frontend). My problem is that I need to know when I press refresh button. I tried many options like that:

    if (window.performance) {
        console.info("window.performance works fine on this browser");
    }
    if (performance.navigation.type == 1) {
        console.info( "This page is reloaded" );
    } else {
        console.info( "This page is not reloaded");
    }

When I run my application and refresh the page I always see "window.performance works fine on this browser" and "This page is not reloaded". So I thought that the best opcion was check if my mouse is in the page or not (document.onmouseover, document.onmouseleave) and detect when my URL changes and in that moment call a Java function for reload my session params and show the page. I don't know if is it the best option. If somebody could say me other way to do that will be really good for me!!

Thanks in advance.

Regards!!

Mr.Z
  • 1
  • 5
  • 2
    Why? A new page is loaded when the URL changes ... Unless you're not playing with `history.replaceState`, your question doesn't tell. – Teemu May 04 '18 at 07:41
  • 3
    Possible duplicate of [How to detect URL change in JavaScript](https://stackoverflow.com/questions/6390341/how-to-detect-url-change-in-javascript) – qxg May 04 '18 at 07:42
  • 1
    @qxg How is that duplicate? – Teemu May 04 '18 at 07:42
  • You can use regex for the same. [Here](https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url) is a link for your reference. – Outlaw May 04 '18 at 07:43
  • @Outlaw A regex won't tell you when a URL has *changed* in the address bar. – deceze May 04 '18 at 07:45
  • @MrZ Usually when the URL changes the browser sends a new request to the server to load a new website. How exactly would your URL change without the page reloading, and why/how/when would you need to detect that? – deceze May 04 '18 at 07:46
  • Related: [How to get notified about changes of the history via history.pushState?](https://stackoverflow.com/questions/4570093/how-to-get-notified-about-changes-of-the-history-via-history-pushstate) – str May 04 '18 at 07:47
  • Thanks for your responses! I have updated the issue, I think that now is clearly. – Mr.Z May 04 '18 at 09:51
  • Do you want to detect [1] change in url path [2] change in url hash [3] page refresh? – Arghya C May 04 '18 at 20:05
  • Thank for reply me Arghya, I need to detect when user press "F5" so will be fine detect change url path or page refresh, What do you think about that? What would be the best option? – Mr.Z May 08 '18 at 06:01

0 Answers0