0
  I am facing a challenge in adding the click event on a link.

    $('#submitDomainName').live('click', function(){
    fnCheckDomainNameExists();
    var myRegEx = new RegExp(/^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}$/);
    userInputDomain = validateUserDomain($('#customDomainName').val());

    if (!userInputDomain.match(myRegEx)) {
            alert("Please enter a valid domain name");
            return;
        }
    if(confirm('Are you sure to move to a custom domain')){ 
            //alert(userCustomDomainNameUpdated);
            $.post('saveCustomDomain.php',{userExistingDomainName:userExistingDomainName,userCustomDomainName:userCustomDomainNameUpdated},function(data){
                $('#popUpContent').html('<div class="alertBoxContainer"><h2>You have Successfully Published your website on your Domain</h2><a style="color:#ffffff;" target="_blank" href="http://'+data+'">'+data+'</a><br class="clear" /></div>');
            })              
        }
     });

In the above click fucntion I want first the fnCheckDomainNameExists() to be executed and checked and then the callback to proceed.The function is below:

 function fnCheckDomainNameExists(){
    userInputDomain = validateUserDomain($('#customDomainName').val());     
    $.post('checkExistingDomain.php',{userInputDomain:userInputDomain},function(data){
                //alert(data);
            if(data=='false'){
                alert('Domain name already in use.Please chose a different one');
                return;
            }
        })

 }

What happens is as I click the link instead of fnCheckDomainNameExists the callback funcion with confirm box "Are you sure to move to a custom domain appears.I want first to check if the domain is new and then execute the callback.
I also tried using it with if(!fnCheckDomainNameExists ())

pes502
  • 1,597
  • 3
  • 17
  • 32
KillABug
  • 1,414
  • 6
  • 34
  • 69
  • This is because your code continues execution whilst the AJAX request is occurring, and the `alert` happens before the request has completed. You need to put the code after `fnCheckDomainExists()` in the callback of the request. – Rory McCrossan Jul 04 '14 at 09:22
  • @RoryMcCrossan Can you elaborate a bit,I did not get you exactly when you said:- put the code after fnCheckDomainExists() in the callback of the request – KillABug Jul 04 '14 at 09:37
  • All the info you need is in the accepted answer of the question Quentin linked to. – Rory McCrossan Jul 04 '14 at 12:27
  • Yes I achieved it using the `.done()` method of jquery which executes only when the request is completely prosessed. Reference: http://api.jquery.com/jquery.ajax/ – KillABug Jul 07 '14 at 13:53

0 Answers0