duangu9997 2014-08-31 18:07
浏览 52
已采纳

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

I've got two functions which both get the user_id of a user. One function gets the session user_id and the other function gets the id that's in the url like so: profile.html?id=123. Here's the code:

// 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();

So, what I need to do is to check if the variable 'url_user_id' is set. If not, the url_user_id should get the same value as the variable 'session_user_id'. However, for some reason, this doesn't happen. I have the if statement in the code above in this part:

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

I use the url_user_id variable to get the username of the current profile the user is viewing. Here's the function that gets the username:

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

When I visit the page with the url like this: www.mywebsite.com/profile.php?id= (so if I specify no id in the URL) the variable url_user_id, which normally gets its value from the URL, should get the value of the variable session_user_id. Currently, I keep getting 'undefined' as the value of url_user_id and the if statement doesn't seem to notice that value to take action.

Who knows how I can fix this?

+=======================================================================================+ EDIT

I'm now able to give the url_user_id the right value, but my function get_username() can't access that value. My get_session_user_id function now looks like this:

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

It alerts url_user_id with the correct value when no id is specified in the URL, but the get_userame function still says 'undefined' when I alert it in that function. Why can't the get_username function get the most recent value of url_user_id?

  • 写回答

1条回答 默认 最新

  • dongzilu0178 2014-08-31 18:43
    关注

    It looks like you're calling get_username immediately, before get_session_user_id has had a chance to return.

    Try doing this:

    // 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();
    

    We've moved the call to get_username inside the success callback to your POST, but otherwise the code is the same.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决