2010-10-27 3 views
0

J'utilise JQueryComment rediriger vers la page quand il y a 404 et 500 erreur sur la fonction de charge jquery

J'ai sous le code JQuery

$(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; 
     });   

}); 

Si vous voyez ci-dessus le code Jquery J'utilise .load fonction et en vérifiant plus avant les erreurs venant sur httprequest. Maintenant que cette fonction .load est écrit sur $ (". Load-fragment"). Each (function() redirige vers la page d'erreur sur la première page de chargement seulement s'il y a une erreur à venir lors de l'envoi httprequest pour tous les liens ayant .load fragment classe sur elle, je veux que devrait être rediriger uniquement si l'utilisateur clique sur ce lien uniquement pas sur la charge de la page.

S'il vous plaît suggérer!

Répondre

0

Je résolu la question ci-dessus ci-dessous modifications de code // Fonction ajoutée pour la fonctionnalité Onglets de destination

$(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 

S'il vous plaît jeter un oeil et de suggérer si des changements requis!

Questions connexes