Q:

javascript variable scope

this might be fairly simple but I cant find my way around... I have js (jQuery) code that goes like this:
function publicCheck(research, $clicked) {
var result = null;
$.ajax({
    type: "POST",
    data: "research="+research+ "&action=check",
    url: "ajax_check.pl",
    success: function (msg) {
            if (something ...) {
            result = "ok";
            }
            });
     return result;
     }

=> now why do I get null returned every single time (I know my condition works ok).  It must be a scope issue.  Why is result not "ok" but is always null even when the condition is valid? 

A:

Not trying to be mean, but you should look a bit before asking questions, this is a very common one, here is an answer to a similar question:
Variable scope in Javascript Object
If you have further doubts, we can help you. 

A:

$.ajax() is a async call, you can't get the result like that unless you provide a parameter
async: false

This is not recommended, and your browser will freeze until you get a response. 
Its better if you provide a callback for return
function publicCheck(research, $clicked, callback) {
    $.ajax({
        type: "POST",
        data: "research=" + research + "&action=check",
        url: "ajax_check.pl",
        success: function (msg) {
            if (something...) {
                callback("ok");
            }
        }
    });
}

USAGE:
publicCheck(research, $clicked, function(status) {
   alert(status); // yeilds ok
});

