1

I am working on some JavaScript/jQuery code that detected a user agent based against a list of specified user agents retrieved via a JSON request.

As soon as a match is found I want the function to return false and to break out of the loop, only continuing to return true if none of the user agents in the JSON document were found.

I have this code...

function checkUserAgent() {
    $.getJSON('noapplet-useragents.json', function(data) {
        $.each(data, function(key, val) {
            if(navigator.userAgent.search(new RegExp(val)) != -1) {
                console.log("False!");
                return false;
            }
        });
        console.log("True!");
        return true;
    });
}

// ... call to checkUserAgent() function 

However, when false is returned; true is also returned. How can I make the first return stop the function, as necessary.

The console.log() statements are just their temporarily for Firebug.

Edit: Thanks to all for their feedback. I could not add comments for some reason.

  • 2
    You cannot return values from an Ajax call. Read: [jQuery: Return data after ajax call success](http://stackoverflow.com/questions/5316697/jquery-return-data-after-ajax-call-success) Also read the [documentation about `$.each`](http://api.jquery.com/jQuery.each/) returning `false` does stop the iteration, but you cannot return a value from the callback. – Felix Kling May 03 '11 at 19:24
  • It seems that the `}` for `$.each` is missing. – morgar May 03 '11 at 19:26
  • @morgar: No, it is just bad indentation. – Felix Kling May 03 '11 at 19:27
  • See my post below :) Looks like @Felix and I were writing the same code at the exact same time... Haha but I think I may have posted sooner? WINNING. – pixelbobby May 03 '11 at 19:33

2 Answers2

2

As I said in my comment, you cannot return a value from an Ajax call. You have to pass a callback:

function checkUserAgent(callback) {
    $.getJSON('noapplet-useragents.json', function(data) {
        var result = true;
        $.each(data, function(key, val) {
            if(navigator.userAgent.search(new RegExp(val)) != -1) {
                console.log("False!");
                result = false;
                return false;
            }
        });
        callback(result);
    });
}

and call it with:

checkUserAgent(function(result) {
    // so something with the result
});
Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
0

One option is for your checkUserAgent function to handle callbacks. This code would work:

var checkUserAgent = function(callback){ //callback is the function
        $.getJSON('noapplet-useragents.json', function(data) {         
            $.each(data, function(key, val) {             
                if(navigator.userAgent.search(new RegExp(val)) != -1) {                 
                    console.log("False!");                 
                    callback(true);         
                }
            });

            callback(false);

}
pixelbobby
  • 4,368
  • 5
  • 29
  • 49