0

I need some help with this for loop.. my code is supposed to run on these specific url's but It seems to skip over the loop and apply to all pages. Thanks

$(document).ready(function () {
    if (isValidUrl()) {
        displayBoot();
        setupBootEvents();
    }
});

function isValidUrl() {
    var isValid = false,
        validUrls = [
            "/sport/btts-match-result", 
            "/sport/double-delight",
            "/sport/recently-paid-out",
            "/promotions",
            "/promotions/Sports",
            "/lotto",
            "/virtual", 
            "/bingo" 
        ];
    for (var i in validUrls) {
        var currentUrl = window.location.pathname + window.location.search;
        if (currentUrl == validUrls[i]) {
            isValid = true;
        }
    }
    //return isValid;
    return true;
}

function displayBoot() {...

function setupBootEvents() {...
  • 2
    [Related](http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea). After you've fixed your code, you will check, what you're actually comparing. Use console to show the values of `currentUrl` and `validUrls[i]`. – Teemu Feb 14 '15 at 11:45
  • 1
    If `validUrl()` always returns `true`, why even check for that? – raina77ow Feb 14 '15 at 11:47
  • None of the URLs in the lookup has a search term so why would you need to concatenate `+ window.location.search`? – Roamer-1888 Feb 14 '15 at 11:54
  • And don't use `in` to iterate over an array. – Roamer-1888 Feb 14 '15 at 11:55

1 Answers1

2

Modify your function a bit

function isValidUrl() {
        validUrls = [
            "/sport/btts-match-result", 
            "/sport/double-delight",
            "/sport/recently-paid-out",
            "/promotions",
            "/promotions/Sports",
            "/lotto",
            "/virtual", 
            "/bingo" 
        ];
    for (var i in validUrls) {
        var currentUrl = window.location.pathname + window.location.search;
        if (currentUrl == validUrls[i]) {
            return true;
        }
    }
    return false;
}
void
  • 36,090
  • 8
  • 62
  • 107
  • And when working with the `location` object, always test in at least one Webkit-based and one Gecko-based browser. – Roamer-1888 Feb 14 '15 at 12:25
  • @Roamer-1888 I guess its fine fine in that case as well.. http://jsfiddle.net/hs8tqxyb/ – void Feb 14 '15 at 12:25
  • @void, you missed the point. The OP's validUrls don't have search terms. – Roamer-1888 Feb 14 '15 at 12:28
  • That depends on him, right? What if `validUrl` is just the `sample` not the whole `dataset`. – void Feb 14 '15 at 12:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70910/discussion-between-void-and-roamer-1888). – void Feb 14 '15 at 12:54