我可以';如果语句在Javascript:S中有效,则似乎不会实现这一点


I can't seem to make this if statement work in Javascript :S

我有两个函数,它们都获取用户的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获取值的变量URL_user_id应该获取变量session_user_id的值。目前,我一直将"undefined"作为url_user_id的值,if语句似乎没有注意到该值需要采取行动。

谁知道我该怎么解决?

+=======================================================================================+编辑

我现在可以给url_user_id一个正确的值,但我的函数get_username()无法访问该值。我的get_session_ser_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函数仍然显示"未定义"。为什么get_username函数不能获取url_user_id的最新值?

在get_session_ser_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();

我们已经在POST的成功回调中移动了对get_username的调用,但除此之外,代码是相同的。

相关文章: