首页 > 解决方案 > window.location.href 不重定向

问题描述

我的功能如下

<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';
    }
}

我刚刚添加了第二个功能check_email_address。该函数被调用并打印日志,但windows.location.href该函数的 没有得到评估,页面不断重定向到window.location.href第一个函数中提到的 url。

标签: javascriptdjango

解决方案


尝试return false;在处理程序的末尾使用。

您可以参考这个问题了解更多详情。

编辑:请试试这个。

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;
}

推荐阅读