简体   繁体   English

加载内容而不刷新

[英]Load content without refresh

I have codeigniter website which is done 1 year ago. 我有codeigniter网站,这是在1年前完成的。 No, I want to implement new HTML5 history API (pushstate) function to enable change content based on the url without header refresh. 不,我想实现新的HTML5历史API(pushstate)功能,以便在没有标题刷新的情况下根据网址启用更改内容。 Is there any simple way, to avoid url restructure and load whole content using new HTML5 history API? 有没有简单的方法,以避免网址重组和使用新的HTML5历史API加载整个内容? I was trying to find some way, but most of them are using get methods. 我试图找到一些方法,但大多数都使用get方法。

$(function() {
        $('nav a').click(function(e) {
    $("#loading").show();
            href = $(this).attr("href");

            loadContent(href);

            // HISTORY.PUSHSTATE
            history.replaceState('', 'New URL: '+href, href);
            e.preventDefault();


        });

        // THIS EVENT MAKES SURE THAT THE BACK/FORWARD BUTTONS WORK AS WELL
        window.onpopstate = function(event) {
            $("#loading").show();
            console.log("pathname: "+location.pathname);
            loadContent(location.pathname);
        };

    });

    function loadContent(url){


        // USES JQUERY TO LOAD THE CONTENT
        $.getJSON("content.php", {cid: url, format: 'json'}, function(json) {
                // THIS LOOP PUTS ALL THE CONTENT INTO THE RIGHT PLACES
                $.each(json, function(key, value){
                    $(key).html(value);
                });
                $("#loading").hide();
            });

        // THESE TWO LINES JUST MAKE SURE THAT THE NAV BAR REFLECTS THE CURRENT URL
        $('li').removeClass('current');
        $('a[href="'+url+'"]').parent().addClass('current');

    }

Here is demo for this : http://sandbox.cergis.com/html5-historyAPI/page1.php 这是演示: http//sandbox.cergis.com/html5-historyAPI/page1.php

I want to implement new HTML5 history API (pushstate) function to enable change content based on the url without header refresh. 我想实现新的HTML5历史API(pushstate)功能,以便在没有标题刷新的情况下根据网址启用更改内容。

pushState is there to allow you to change the url without loading a new page. pushState允许您不加载新页面的情况下更改URL

Is there any simple way, to avoid url restructure and load whole content using new HTML5 history API? 有没有简单的方法,以避免网址重组和使用新的HTML5历史API加载整个内容?

Changing the content is the job of DOM. 改变内容是DOM的工作。

You can use XMLHttpRequest (or other Ajax techniques) to load new content from the server (and then insert that into the page using DOM manipulation) 您可以使用XMLHttpRequest(或其他Ajax技术)从服务器加载新内容(然后使用DOM操作将其插入页面)

Generally, you should use the history API in combination with DOM manipulation. 通常,您应该将历史API与DOM操作结合使用。 You change the content of the page with DOM, then change the URL to one that the server can use to generate the same page from scratch. 您使用DOM更改页面的内容,然后将URL更改为服务器可用于从头开始生成相同页面的URL。 This means the page still works for users (including search engines) without JavaScript, and it avoids the homepage loading before being replaced (after a few seconds) with different content. 这意味着该页面仍然适用于没有JavaScript的用户(包括搜索引擎),并且它在使用不同内容替换(几秒钟之后)之前避免了主页加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM