How to detect URL change in SPA


Deepak Ingle

Notice

The solution should use pure Javascript - no external libraries and frameworks required.


In a SPA, everything is routed using a routing mechanism. I only want to listen to even numbers when any part of the url changes (not just the hash. Any changes I want to detect)

Following is an example of SPA,

https://www.google.in/
https://www.google.in/women 
https://www.google.in/girl 

Now I want to catch the event whenever the url changes from https://www.hopscotch.in/ to https://www.hopscotch.in/women .

I tried,

window.addEventListener("hashchange",function(event){
    console.log(this); // this gets fired only when hash changes
});
Quentin

Normally, nothing happens when the URL changes. You are loading a new document (although there are load, and so on)

If you were to set a new URL using JavaScript (i.e. via pushState) , no event would occur, but you don't need this event because you've explicitly written the code in there, so just add anything else in that code i.e. Can.

If the URL is changed via pushState history via the browser back button or similar, you will receive an popstateevent .


So there is no good general way to hook into every SPA. You might come closest to using setIntervaland checking the value location.hrefto see if it has changed since the last check.

Related


How to detect URL change in SPA

Deepak Ingle Notice The solution should use pure Javascript - no external libraries and frameworks required. In a SPA, everything is routed using a routing mechanism. I only want to listen to even numbers when any part of the url changes (not just the hash. An

How to detect URL change in SPA

Deepak Ingle Notice The solution should use pure Javascript - no external libraries and frameworks required. In a SPA, everything is routed using a routing mechanism. I only want to listen to even numbers when any part of the url changes (not just the hash. An

How to detect URL change in SPA

Deepak Ingle notes The solution should use pure Javascript - no external libraries and frameworks required. In a SPA, everything is routed using a routing mechanism. I just want to listen once when any part of the url changes (not just the hash. Any changes I

How to detect URL change in SPA

Deepak Ingle Notice The solution should use pure Javascript - no external libraries and frameworks required. In a SPA, everything is routed using a routing mechanism. I only want to listen to even numbers when any part of the url changes (not just the hash. An

JS: Detect URL change in SPA

wasddd_ I would like to hear about path changes in my unmaintained SPA. I found a solution here : https://stackoverflow.com/a/44819548/7042552 However, this still seems "hacky" to me - but my implementation still looks like this: let url = window.location.href

JS: Detect URL change in SPA

wasddd_ I would like to hear about path changes in my unmaintained SPA. I found a solution here : https://stackoverflow.com/a/44819548/7042552 However, this still seems "hacky" to me - but my implementation still looks like this: let url = window.location.href

JS: Detect URL change in SPA

wasddd_ I would like to hear about path changes in my unmaintained SPA. I found a solution here : https://stackoverflow.com/a/44819548/7042552 However, this still seems "hacky" to me - but my implementation still looks like this: let url = window.location.href

Detect URL changes in SPA

wsddd_ I'd like to hear about path changes in a SPA that I don't maintain. I found a solution here : https://stackoverflow.com/a/44819548/7042552 However, it seems a bit "hacky" to me - but my implementation still looks like this: let url = window.location.hre

Detect URL changes in SPA

wsddd_ I'd like to hear about path changes in a SPA that I don't maintain. I found a solution here : https://stackoverflow.com/a/44819548/7042552 However, it seems a bit "hacky" to me - but my implementation still looks like this: let url = window.location.hre

How to detect URL change in JQuery?

username What I'm trying to do is refresh localStoragewhen the user moves to a different page . For example, let's say I'm currently at http://foo.com/accounts/mypage . I want to refresh when the user moves to the url http://foo.com/album .localStorage Here is

Detect URL change in SFSafariViewController

username I have a SFSafariViewController that opens when the user clicks a link in my app. I need to detect when the URL changes so that when the URL changes, the app shows an alert. How can we detect URL change in SFSafariViewController? give You can't do thi

Detect URL change in SFSafariViewController

username I have a SFSafariViewController that opens when the user clicks a link in my app. I need to detect when the URL changes so that when the URL changes, the app shows an alert. How can we detect URL change in SFSafariViewController? give You can't do thi

Detect URL change in SFSafariViewController

username I have a SFSafariViewController that opens when the user clicks a link in my app. I need to detect when the URL changes so that when the URL changes, the app shows an alert. How can we detect URL change in SFSafariViewController? give You can't do thi

Detect URL change in python

Linski I need to detect the url change when the store is on sale, I am using the requests library with no luck, even the fire is still returning No deals on todayand the check value is still[u'http:', u'', u'www.dealwebsite.co', u'Electroshop'] Main store URLh

Python detect url change?

username I'm creating a script that connects to wordpress and detects if the login is not correct, so I don't like the "if contains" approach, so I try to use this script: when you visit this url in a wordpress site: http://www.example.com/blog/wp-admin/profil

Detect URL change in SFSafariViewController

username I have a SFSafariViewController that opens when the user clicks a link in my app. I need to detect when the URL changes so that when the URL changes, the app shows an alert. How can we detect URL change in SFSafariViewController? give You can't do thi

Detect URL change in SFSafariViewController

username I have a SFSafariViewController that opens when the user clicks a link in my app. I need to detect when the URL changes so that when the URL changes, the app shows an alert. How can we detect URL change in SFSafariViewController? give You can't do thi

Detect URL change in python

Linski I need to detect the url change when the store is on sale, I am using the requests library with no luck, even the fire is still returning No deals on todayand the check value is still[u'http:', u'', u'www.dealwebsite.co', u'Electroshop'] Main store URLh

Python detect url change?

username I'm creating a script that connects to wordpress and detects if the login is not correct, so I don't like the "if contains" approach, so I try to use this script: when you visit this url in a wordpress site: http://www.example.com/blog/wp-admin/profil

How to detect url change via AJAX without reloading in jQuery?

Roberto Thomas I have a user script (for Chrome) that formats a practice page for duolingo. It only works for one line of CSS jQ(document).ready(function(){ jQ('#start-button').css({"float": "left", "margin-right": "10em"}); }); This works fine the first

SwiftUI WKWebView detect url change

Alparslan Selcuk Develioglu I am an agile learner. I'm using SwiftUI and it's a struct where I have to implement WKWebView and the url changes dynamically. I have to catch these changing urls, but the solutions I've tried don't work. For example: https://stack

Detect URL change in JavaFX WebView

username In JavaFX, WebViewI am struggling to detect URL changes. I have this method in my class: public Object urlchange() { engine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() { @Override public void changed(Obs

SwiftUI WKWebView detect url change

Alparslan Selcuk Develioglu I am an agile learner. I'm using SwiftUI and it's a struct where I have to implement WKWebView and the url changes dynamically. I have to catch these changing urls, but the solutions I've tried don't work. For example: https://stack

Detect URL change in JavaFX WebView

username In JavaFX, WebViewI am struggling to detect URL changes. I have this method in my class: public Object urlchange() { engine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() { @Override public void changed(Obs

Detect URL change in Webview android

Abishek Maharajpe I am using android webview and load url. In the webview, there is some click event, and on click, the UI in the webview changes. I don't get any callback in ShouldOverrideUrlLoading(...) . Is there a way to detect changes inside webview in an

Detect URL change in WPF WebBrowser

Marcin Limanski Using WebBrowser in WPF, how to check for changes in URL? Can I be fired when the conditions are met? Below, I have a button event that will be set App.browserLinkCheckto the target URL, and open the WebBrowser instance. private void btNextWelc

Detect URL change in JavaFX WebView

username In JavaFX, WebViewI am struggling to detect URL changes. I have this method in my class: public Object urlchange() { engine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() { @Override public void changed(Obs

SwiftUI WKWebView detect url change

Alparslan Selcuk Develioglu I am an agile learner. I'm using SwiftUI and it's a struct where I have to implement WKWebView and the url changes dynamically. I have to catch these changing urls, but the solutions I've tried don't work. For example: https://stack

Detect URL change in Webview android

Abishek Maharajpe I am using android webview and load url. In the webview, there is some click event, and on click, the UI in the webview changes. I don't get any callback in ShouldOverrideUrlLoading(...) . Is there a way to detect changes inside webview in an