You cannot "return" the value to the called function; because, as you understand, the AJAX call is asynchronous. Instead, you need to effectively delay the execution of the code following the AJAX call; putting this code in a function callback.
, , , AJAX , value.
function CheckExistance(userName, callback) {
$.get(
"JQueryPage.aspx", { name: userName },
function(result) {
var value = false;
if (result == "1") {
value = true;
}
callback(value);
}
);
}
, :
function validateUserName() {
var input = $('someField').val();
var isUnique = CheckExistance(input);
if (isUnique) {
} else {
alert("This username is already taken");
};
};
:
function validateUserName() {
var input = $('someField').val();
CheckExistance(input, function (isUnique) {
if (isUnique) {
} else {
alert("This username is already taken");
};
});
};