首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当jquery加载函数出现404和500错误时,如何重定向到页面

当jquery加载函数出现404和500错误时,如何重定向到页面
EN

Stack Overflow用户
提问于 2010-10-27 06:17:44
回答 1查看 784关注 0票数 0

我正在使用JQuery

我有下面的JQuery代码

代码语言:javascript
复制
$(document).ready(function() 
{
        //Looking for each element in the file which has a class "load-fragment" in it
        $(".load-fragment").each(function()         
        {          
            var $objThis = $(this);
            var fname = $objThis.attr("href"); //Getting the href of the element
            var dynDivID = "divContent"+ $objThis.attr("id");  //Name of the dynamic div ID
            var newDiv = $("<div>").attr("id",dynDivID)
            .load(fname+ " #tab-container", {pupdate:"true"},function(response, status, xhr) 
            {
                if (status == "error") 
                {
                    //var msg = "Sorry but there was an error: ";
                    //newDiv.append(msg + xhr.status + " " + xhr.statusText);

                    window.location.href = "www.myerrorpage.aspx";
                }

            })//Loading page fragment from the given link
            .hide()//Hiding all the newly created DIVs
            .addClass('dynDiv')//Adding CSS to newly created Dynamic Divs
            .append($('<img/>').attr({ src: '/system/Images/ajax-loader-circle-thickbox.gif', alt: '', style:'margin:50px 0px 50px 185px' }));//Adding the loading.gif file
            $("#container-4").append(newDiv);//adding new div in div column2
        });    
        $(".load-fragment").click(function(event) 
        {
            // load page on click  
            var $thiz = $(this); //making the current object    
            $thiz.attr("href", "#");         
            $(".tabs-nav li").removeClass("tabs-selected"); //removing the css from the li
            $thiz.parent().addClass("tabs-selected"); //adding the selected class to the parent on click
            $("#tab-container").hide(); //playing with hide and show
            $(".dynDiv").hide();
            $("#divContent" + $thiz.attr("id")).show(); 
            return false;   
        });           

}); 

如果您在上面看到Jquery代码,我将使用.load函数并进一步检查httprequest上出现的错误。现在,在$(".load-fragment").each(function()上编写这个.load函数时,只有在发送包含.load-fragment类的链接时出现任何错误时,才会重定向到第一个页面加载上的错误页面,我希望只有当用户单击该链接而不是加载页面时,才应该重定向。

请给我建议!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-10-28 03:46:31

我通过下面的代码更改解决了上面的问题。//函数为目标Tabs功能添加

代码语言:javascript
复制
$(document).ready(function() 
{
        //Looking for each element in the file which has a class "load-fragment" in it
        $(".load-fragment").each(function()         
        {          
            var $objThis = $(this);
            var fname = $objThis.attr("href"); //Getting the href of the element
            var dynDivID = "divContent"+ $objThis.attr("id");  //Name of the dynamic div ID
            var newDiv = $("<div>").attr("id",dynDivID)
            .load(fname+ " #tab-container", {pupdate:"true"},function(response, status, xhr) 
            {
                //Checking whether response contains class "formContainer" in it or not
                if( $(response).find('#tab-container').find('.formContainer').length ) 
                {                    
                    ($objThis).unbind("click"); //Removing the attached click event                                                  
                }      
                if (status == "error") 
                {               
                    newDiv.removeClass('dynDiv');
                    newDiv.addClass('errorDiv');
                }
            })//Loading page fragment from the given link
            .hide()//Hiding all the newly created DIVs
            .addClass('dynDiv')//Adding CSS to newly created Dynamic Divs
            .append($('<img/>').attr({ src: '/system/Images/ajax-loader-circle-thickbox.gif', alt: '', style:'margin:50px 0px 50px 185px' }));//Adding the loading.gif file
            $("#container-4").append(newDiv);//adding new div in div column2
        });    
        $(".load-fragment").click(function(event) 
        {
            // load page on click  
            var $thiz = $(this); //making the current object   
            var link =  $thiz.attr("href");

            $(".tabs-nav li").removeClass("tabs-selected"); //removing the css from the li
            $thiz.parent().addClass("tabs-selected"); //adding the selected class to the parent on click
            $("#tab-container").hide(); //playing with hide and show
            $(".dynDiv").hide();

            if ( $("#divContent" + $thiz.attr("id")).hasClass("errorDiv") )
            {
                $("#divContent" + $thiz.attr("id")).show();                         
                setTimeout(function(){location.href=link;},200);    
            }
            else
            {            
                $("#divContent" + $thiz.attr("id")).show(); 
                $thiz.attr("href", "#");  
                $(".errorDiv").hide(); 
            }
            return false;   
        });           

}); 
//End of function

如果需要更改,请看一看并提出建议!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4030462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档