0

my functions are as follows

<a onClick="check_claims(this)"  type="button" href="javascript:void(0)" redirurl="www.facebook.com" >Invite</a>



function check_claims(ele){
    var select_claims = document.getElementById('select_claims').value;
    if (select_claims == '1') {
        var result = confirm("do you wish to continue?");
         if (!result) {
                 check_email_address();
                 return;
         }
    }
         check_email_address();
         window.location.href = ele.getAttribute('redirurl');
}


function check_email_address(){
    if ('{{broker.email_address}}' == 'None') {
        console.log('this worked till here');
        window.location.href = 'www.google.com';
    }
}

I just added the second function check_email_address. the function gets called and the log gets printed but the windows.location.href of this function does not get evaluated and the page keeps redirecting to url mentioned in the window.location.href of first function.

Irfan Harun
  • 979
  • 2
  • 16
  • 37
  • 1
    because in javascript it will not wait for `check_email_address` to execute, and direclty next line is redirection so it will redirect from first function before second function redirection . Also where is `select_claims` ??? – Devsi Odedra Nov 02 '20 at 07:37
  • i also tried replacing the `check_email_address();` line with the if condition of `check_email_address` function. That way, javascript will ideally have to execute the next line and thereby should end up redirecting to google.com but that also did not work. – Irfan Harun Nov 02 '20 at 07:46

1 Answers1

0

Try using return false; at the end of the handler.

You can refer to this question for more details.

EDIT: Try this please.

function check_claims(ele){
    var select_claims = document.getElementById('select_claims').value;
    if (select_claims == '1') {
        var result = confirm("do you wish to continue?");
         if (!result) {
                 check_email_address();
                 return;
         }
    }
         var email_check = check_email_address();
         if (!email_check) window.location.href = ele.getAttribute('redirurl');
}


function check_email_address(){
    if ('{{broker.email_address}}' == 'None') {
        console.log('this worked till here');
        window.location.href = 'www.google.com';
        return true;
    }
    return false;
}
Ozgur Sar
  • 2,067
  • 2
  • 11
  • 23