我回来后的动态网页

时间:2015-02-06 08:12:32

标签: jquery html ajax dynamic history

我是一个相对较新的网页设计师。我目前的问题是我有一个动态加载内容和修改历史记录的网页。当网页从一个页面转移到另一个页面时,我能够动态地更改内容,但是当我离开网站并使用历史记录返回时,我一直找不到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>

2 个答案:

答案 0 :(得分: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);
              }
            });
      });

答案 1 :(得分:0)

使用哈希,以便底层Web服务器不会尝试在那里找到实际文档。请记住,Web服务器路由器首先运行。此外,您还需要检查加载时的哈希值,以便在从历史记录重新加载或直接访问时提取正确的内容。试试这个:

$(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>
相关问题