繁体   English   中英

如果在Javascript中运行语句,我似乎无法做到这一点:

[英]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;      
        }

当我使用如下网址访问网页时:www.mywebsite.com/profile.php?id=(因此,如果我在网址中未指定ID),通常从网址中获取其值的变量url_user_id应该获取session_user_id的值。 目前,我不断获得“未定义”作为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函数仍会显示“未定义”。 为什么get_username函数不能获取url_user_id的最新值?

好像您在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,但其他代码相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM