编程语言
首页 > 编程语言> > 在Firefox中工作的javascript函数,但在Google chrome中不工作

在Firefox中工作的javascript函数,但在Google chrome中不工作

作者:互联网

我做了一个简单的功能,它可以完美地在Firefox中运行,但不能在google chrome中运行.它给hide()函数带来问题,并显示正在加载图像,其余的函数正常工作.

function setPrivacyToCustom(){
    $("form#email_contacts_form #gmail_import_btn").unbind();
    $("form#email_contacts_form #gmail_import_btn").click(function()
            {
                var current_album_id = $(this).siblings('input#selected_album_id').val();
                $(this).hide();
                var loading_img = addLoadingImage( $(this), "before", 'loading_medium_purple.gif',61, 32 );
                var usersList = "";
                var total = $("input[name='emails[]']:checked:enabled").length;
                $("input[name='emails[]']:checked:enabled").each(function(index) 
                {
                    if (index === total - 1) 
                    {
                        // this is the last one
                        usersList += $(this).attr('rel');
                    }
                    else
                    {   
                        usersList += $(this).attr('rel')+",";
                    }   
                });
                var thisss = $(this);
                $.ajax({
                    async: false,
                    url : "/" + PROJECT_NAME + "profile/set-custom-privacy-and-viewers-for-album",
                    method : "POST",
                    data :{"custom_viewer" : usersList, "album_id":current_album_id},
                    type : "post",
                    dataType : "json",
                    success : function(jsonData)
                    {                    
                        if(jsonData == 1)
                        {
                            $("span#"+loading_img).remove();
                            $('div#gmail_popup').bPopup().close();
                            $("span#"+loading_img).remove();
                            $(thisss).fadeIn();
                            $(".alert-box").remove();
                            $(".alert-box1").remove();
                            $(".alert-box2").remove();
                            showDefaultMsg( " Your setting changes have been saved.", 1 );

                        }
                        else
                        {
                            alert("Some error has occured.Please select custom user again.");
                        }

                    }

                });

            });
}

以上是我编写的代码,

 $(this).hide();
  var loading_img = addLoadingImage( $(this), "before", 'loading_medium_purple.gif',61, 32 );

这两行在隐藏按钮和显示加载图像的地方不起作用.

显示加载图像的功能如下

function addLoadingImage(elem, position, image_name, width, height)
{
    image_name = typeof image_name !== 'undefined' ? image_name : "loading_small_purple.gif";
    width = typeof width !== 'undefined' ? width : "0";
    height = typeof height !== 'undefined' ? height : "0";

    var unique_num = new Date().getTime();
    var obj = "<span class = 'loading' id = '"+unique_num+"' ><table><tr><td style = 'width:"+width+"px; height:"+height+"px'><img src = '/" + PROJECT_NAME + "public/images/" + image_name + "' alt = 'Wait...' /></td></tr></table></span>";
    elem.siblings("span.loading").remove();
    if( position == "before" )
    {
        $(obj).insertBefore( elem );
    }
    else if( position == "after" )
    {
        $(obj).insertAfter( elem );
    }
    return unique_num;
}

上面的代码可以完美地在狐狸中工作,但不能在谷歌浏览器中工作:(

解决方法:

发生这种情况是因为async:false.因为当我使我的ajax调用异步为false时,它停止了其他即将发生的事件.在编写这样的代码之前,我应该知道asyn:false的正确实现.谢谢@sunny,@ashish kumar的帮助.

标签:show-hide,hide,javascript
来源: https://codeday.me/bug/20191029/1963136.html