2015-02-06 2 views
2

저는 비교적 새로운 웹 페이지 디자이너입니다. 현재 문제는 콘텐츠를 동적으로로드하고 기록을 수정하는 웹 페이지가 있다는 것입니다. 나는 웹 페이지가 페이지에서 페이지로 이동함에 따라 동적으로 컨텐트를 변경할 수 있지만, 웹 사이트를 떠나 역사를 다시 사용할 때 404 페이지를 찾을 수 없다. 이 상황을 해결할 방법이 있습니까?내가 돌아온 후 동적 웹 페이지

<script type="text/javascript"> 
$(document).ready(function(){ 
    $(function() { 
     $('#bodywrap').load('/common/index.php'); 

     $('ul#nav li a').click(function(e) { 
      $("#loading").show(); 
      href = $(this).attr("href"); 
      loadContent(href); 
      // HISTORY.PUSHSTATE 
      history.pushState('', '', href); 
      e.preventDefault(); 
     }); 
     $(window).bind('popstate', function(event) { 
      $("#loading").show(); 
      console.log("pathname: "+location.pathname); 
      loadContent(location.pathname); 
     }); 
    }); 
    function loadContent(url){ 
     // USES JQUERY TO LOAD THE CONTENT 
     $('#bodywrap').load('/common/'+url+'.php'); 
     $("#loading").hide(); 
     $('li').removeClass('active'); 
     $('a[href="'+url+'"]').parent().addClass('active'); 
    } 
}); 
</script> 
+0

내가이 작업을 수행하는 다른 방법의 인식입니다. 당신이 약간의 통찰력을 제공 할 수 있다면 나는 그것을 좋아할 것입니다 :) @BitwiseCreative –

+0

지나치게 가정적이고 극적 이었기 때문에 제 의견을 삭제했습니다. @ 바네트 그로버 –

+0

하하 걱정 마, 나는 이것에 대한 새로운 말로 그것을 사용하는 방법을 이해하려고 :) @ BitwiseCreative –

답변

0

기본 웹 서버가 실제 문서를 찾지 않도록 해시를 사용합니다. 웹 서버 라우터가 먼저 실행됩니다. 또한 히스토리에서 다시로드하거나 직접 액세스 한 경우 올바른 내용을 가져 오려면로드시 해시를 확인해야합니다. 이 시도 : 귀하의 링크 해시해야합니다

$(document).ready(function(){ 
    $(function() { 
     $('#bodywrap').load('common/index.php'); 
     // Handle page load 
     loadHref(); 
     // handle link click 
     $('ul#nav li a').click(function(e) { 
      $("#loading").show(); 
      href = $(this).attr("href").substr(1); 
      loadContent(href); 
      // HISTORY.PUSHSTATE 
      history.pushState('', '', $(this).attr("href")); 
      e.preventDefault(); 
     }); 
     // Handle popstate 
     $(window).bind('popstate', function(event) { 
      $("#loading").show(); 
      loadHref(); 
     }); 
    }); 
    function loadContent(url){ 
     // USES JQUERY TO LOAD THE CONTENT 
     $('#bodywrap').load('common/'+url+'.php'); 
     $("#loading").hide(); 
     $('li').removeClass('active'); 
     $('a[href="'+url+'"]').parent().addClass('active'); 
    } 
    function loadHref() { 
     var href; 
     var loadHref = window.location.href; 
     if (loadHref.indexOf('#') > -1) { 
      href = loadHref.substr(loadHref.lastIndexOf('#') + 1); 
      loadContent(href); 
     } 
    } 
}); 

참고 :

<a href="#about">About</a> 
+0

이것은 같은 방법으로로드하는 것 같습니다. 그것은 내가 404 오류를 얻지 못하는 문제를 해결하지만이 경우 localhost : 8888/#에 대한 링크를 넣어 준다면 빈 페이지 –

+0

수정을 제공합니다 : 지금은 작동하는 것처럼 보이지만 여러 번 새로 고쳐야합니다. . 왜 그런지 알겠습니까? –

+0

네, 왜냐하면 해시 URL이'popstate' 이벤트에서 처리되지 않았기 때문입니다. 코드를 업데이트했습니다. –

0

난 당신이 일부 변경이 코드를 사용할 수 있습니다 내 프로젝트 http://forkphp.com, 동일한 기능을 만들었습니다.

$(document).on('click','.changepage',function() { 

      // body... 
      prev = window.location; 
      next = "<?php echo $this->appBase(); ?>"+url; // url to load 
      var url = 'YOUR_URL'; 
      var target = $(this).attr('target'); // body in your case 
     $.ajax(
      { 
       url:"<?php echo $this->appBase(); ?>"+url, 
       method:'POST', 
       data:{terminal:1}, 
       dataType:'json', 
       beforeSend:function(){ 
       // $(target).disablify(); // show loader 
       }, 
       success:function(data) 
       { 

       $(target).html(data.html); 
       document.title = data.title; 
       // $(target).enablify(); // hide loader 
       window.history.pushState({"html":data.html,"pageTitle":"history"},"", "<?php echo $this->appBase(); ?>"+url); 
       } 
      }); 
     }); 
+0

내 대신이 코드를 사용합니까? –

+0

예, 가능합니다. 코드에서 기본 url 메서드를 바꾸면됩니다. –

관련 문제