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 my JQueryscript.

$(window).unload(function(){
    if ((window.location.pathname).indexOf('mypage') < 0) {
        localStorage.flush();
    }
});

But it doesn't work...

Any other good ideas?

Lajos Arpad

Add the clickhandler to your link like this:

var lastLink = "";
$("body").on("click", "a", function() {
    lastLink = $(this).attr("href");
});

This clickwill run before the unloadevent and you will be able to use it lastLinkas your value. Just make sure you consider relative URLs as well.

//TODO: implement function isMine
$(window).unload(function() {
    if (isMine(lastLink)) {
        localStorage.clear();
    }
});

Related


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

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

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 value change in input jQuery?

Ottoman I'm currently using this function to detect any changes made to an input box in jQuery. $(document).on("input change paste keyup", "input, textarea, select", function(e) { //do something }); The problem is that the function won't run when you chan

How to detect value change in input jQuery?

Ottoman I'm currently using this function to detect any changes made to an input box in jQuery. $(document).on("input change paste keyup", "input, textarea, select", function(e) { //do something }); The problem is that the function won't run when you chan

How to detect value change in input jQuery?

Ottoman I'm currently using this function to detect any changes made to an input box in jQuery. $(document).on("input change paste keyup", "input, textarea, select", function(e) { //do something }); The problem is that the function won't run when you chan

How to detect value change in input jQuery?

Ottoman I'm currently using this function to detect any changes made to an input box in jQuery. $(document).on("input change paste keyup", "input, textarea, select", function(e) { //do something }); The problem is that the function won't run when you chan

How to detect value change in input jQuery?

Ottoman I'm currently using this function to detect any changes made to an input box in jQuery. $(document).on("input change paste keyup", "input, textarea, select", function(e) { //do something }); The problem is that the function won't run when you chan

How to detect part of current url using jquery?

peace love Is it possible to detect a value from the current URL with jquery? E.g: http://example.com/home.php?name=john From this url, I need to know name == john: $(document).ready(function() { if (window.location.name == john) alert("my name

How to detect part of current url using jquery?

peace love Is it possible to detect a value from the current URL with jquery? E.g: http://example.com/home.php?name=john From this url, I need to know name == john: $(document).ready(function() { if (window.location.name == john) alert("my name

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

jQuery detect DPI change

Daniel I'm trying for half a day now, to detect jQuery's DPI change. The situation is as follows: I have a MacBook Pro (retina) and a regular screen attached to it. I want to detect changes in DPI when I move browser windows from regular windows to MacBooks. O

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

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

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