Run 'if url contains' conditional function without page refresh

I have a single page app where part of the url changes but the page does not refresh.

I have a simple function that needs to run, only when 'foo' appaers in the URL.

This obviously does not work because it only runs the first time the page loads, and 'foo' may not be there yet.

if (window.location.href.indexOf("foo") > -1) {
//stuff
}

Is there a way I can detect a URL change in order to run my function? Or a better way? The URLs in question do not contain a hash, so hashchange isn't working.

Answers


You could use a while loop that runs till it finds what you need like this:

var found =false;
while(found == false){

    if (window.location.href.indexOf("foo") > -1) {
    //stuff
        found = true;
        //more code 
     }

} 

Need Your Help

Grouping smallest time per group in SQL

sql postgresql greatest-n-per-group gaps-and-islands

This is a very simple question and I feel like the answer should be obvious, but it's been two hours I'm not finding a solution.

Android ExpandableListView how to load the data only once

android expandablelistview

I load some data from internet to an ExpandableListView, but if I turn to another Activity and come back, data load from internet again.