我似乎无法在Javascript:S中使用if语句

时间:2014-08-31 18:07:26

标签: javascript php ajax

我有两个函数都可以获取用户的user_id。一个函数获取会话user_id,另一个函数获取url中的id,如下所示:profile.html?id = 123。这是代码:

// get URL parameter (user_id)
        function GetURLParameter(sParam) {
            var sPageURL = window.location.search.substring(1);
            var sURLVariables = sPageURL.split('&');
            for (var i = 0; i < sURLVariables.length; i++) 
            {
                var sParameterName = sURLVariables[i].split('=');
                if (sParameterName[0] == sParam) 
                {
                    return sParameterName[1];
                }
            }
        }

        // get URL parameter (user_id) function call
        url_user_id = GetURLParameter('id');
        alert("url user id = " + url_user_id); 

        // get SESSION user_id
        function get_session_user_id() {
            $.post( "http://localhost/snapll_back/account/session_user_id.php",
            function(info) {        
                if(info != "Something went wrong. ERROR{1_RAI_ERROR}") {
                    session_user_id = info;
                    alert("session user id = " + session_user_id);

                    // hide the follow button if this me is owned by the viewer
                    if(session_user_id == url_user_id) {
                        $("#follow_button").hide();
                    }

                    // THE QUESTION ON S.O. IS ABOUT THIS IF STATEMENT
                    // give the url_user_id variable the value of the session_user_id variable if not set correctly via the URL
                    if(url_user_id == 'undefined' || url_user_id == "" || url_user_id == null) {
                        url_user_id = session_user_id;  
                    }
                }
                else {
                    $('#warning').html(info).fadeIn(200);
                    setTimeout(function () {
                       window.location.href = "index.html";
                    }, 2000);
                }
            });
            return false;   
        }

        get_session_user_id();

所以,我需要做的是检查是否设置了变量'url_user_id'。如果不是,则url_user_id应该与变量'session_user_id'获得相同的值。但是,出于某种原因,这不会发生。我在上面的代码中有if语句:

if(url_user_id == 'undefined' || url_user_id == "" || url_user_id == null) {
    url_user_id = session_user_id;  
}

我使用url_user_id变量来获取用户正在查看的当前个人资料的用户名。这是获取用户名的函数:

// get username function
        function get_username() {
            $.post( "http://localhost/snapll_back/account/username.php?id="+url_user_id,
            function(info) {        
                if(info != "Something went wrong. ERROR{1_RAI_ERROR}") {
                    $("#main_header_username").html(info);
                }
                else {
                    $('#warning').html(info).fadeIn(200);
                }
            });
            return false;      
        }

当我访问带有这样的URL的页面时:www.mywebsite.com/profile.php?id =(所以如果我在URL中没有指定id)变量url_user_id,它通常从URL获取其值,应该获取变量session_user_id的值。目前,我继续将'undefined'作为url_user_id的值,if语句似乎没有注意到要采取行动的值。

谁知道如何解决这个问题?

+ ============================================== ========================================= + 修改

我现在能够为url_user_id提供正确的值,但我的函数get_username()无法访问该值。我的get_session_user_id函数现在看起来像这样:

// get SESSION user_id
        function get_session_user_id() {
            $.post( "http://localhost/snapll_back/account/session_user_id.php",
            function(info) {        
                if(info != "Something went wrong. ERROR{1_RAI_ERROR}") {
                    session_user_id = info;
                    alert("session user id = " + session_user_id);

                    // if url_user_id is not defined by the URL give it the value of session_user_id
                    if(url_user_id == 'undefined' || url_user_id == "" || url_user_id == null) {
                        url_user_id = session_user_id;  
                        alert("url user id = " + url_user_id); 
                    }

                    // hide the follow button if this me is owned by the viewer
                    if(session_user_id == url_user_id) {
                        $("#follow_button").hide();
                    }
                }
                else {
                    $('#warning').html(info).fadeIn(200);
                    setTimeout(function () {
                       window.location.href = "index.html";
                    }, 2000);
                }
            });
            return false;   
        }

当URL中没有指定id时,它会使用正确的值警告url_user_id,但是当我在该函数中提醒时,get_userame函数仍然显示为“undefined”。为什么get_username函数不能获取url_user_id的最新值?

1 个答案:

答案 0 :(得分:1)

看起来你在get_session_user_id有机会返回之前立即调用了get_username。

尝试这样做:

// get URL parameter (user_id)
    function GetURLParameter(sParam) {
        var sPageURL = window.location.search.substring(1);
        var sURLVariables = sPageURL.split('&');
        for (var i = 0; i < sURLVariables.length; i++) 
        {
            var sParameterName = sURLVariables[i].split('=');
            if (sParameterName[0] == sParam) 
            {
                return sParameterName[1];
            }
        }
    }

    // get URL parameter (user_id) function call
    url_user_id = GetURLParameter('id');

    // get SESSION user_id
    function get_session_user_id() {
        $.post( "http://localhost/snapll_back/account/session_user_id.php",
        function(info) {        
            if(info != "Something went wrong. ERROR{1_RAI_ERROR}") {
                session_user_id = info;
                //alert("session user id = " + session_user_id);

                // if url_user_id is not defined by the URL give it the value of session_user_id
                if(url_user_id == 'undefined' || url_user_id == "" || url_user_id == null) {
                    url_user_id = session_user_id;  
                    //alert("url user id = " + url_user_id); 
                }

                // hide the follow button if this me is owned by the viewer
                if(session_user_id == url_user_id) {
                    $("#follow_button").hide();
                }
            }
            else {
                $('#warning').html(info).fadeIn(200);
                setTimeout(function () {
                   window.location.href = "index.html";
                }, 2000);
            }

            // get username function call
            get_username();

        });
        return false;   
    }

    get_session_user_id();  

    // get username function
    function get_username() {
        //alert(url_user_id);
        $.post( "http://localhost/snapll_back/account/username.php?id="+url_user_id,
        function(info) {        
            if(info != "Something went wrong. ERROR{1_RAI_ERROR}") {
                $("#main_header_username").html(info);
            }
            else {
                $('#warning').html(info).fadeIn(200);
            }
        });
        return false;      
    }

    /*
    * function calls
    */
    // check session function call
    check_session();

我们已将成功回调中的get_username调用移至您的POST,但代码是相同的。