简体   繁体   English

需要帮助了解如何ajaxify一个网站

[英]Need help understanding how to ajaxify a web site

I recently found this gist on how to Ajaxify a Website with the HTML5 History API using History.js, jQuery and ScrollTo: https://github.com/browserstate/ajaxify 我最近发现了如何使用History.js,jQuery和ScrollTo使用HTML5 History API对网站进行Ajax化的要点: https//github.com/browserstate/ajaxify

I am having a hard time getting a simple version of this working and am not sure I understand everything. 我很难得到这个工作的简单版本,我不确定我理解一切。 First, I loaded all the scripts provided in the gist, then set up a really simple nav and content section: 首先,我加载了gist中提供的所有脚本,然后设置了一个非常简单的导航和内容部分:

 <ul id="nav">
    <li id="home-link"><a href="/" title="Home">Home</a>/</li>
    <li id="work-link"><a href="/work" title="Work">Work</a>/</li>
    <li id="labs-link"><a href="/labs" title="Labs">Labs</a>/</li>
    <li id="blog-link"><a href="/blog" title="Blog">Blog</a>/</li>
    <li id="contact-link"><a href="/contact" title="Contact">Contact</a></li>
</ul>

<div id="content"></div>

Next, I changed the selector variables to match the html: 接下来,我更改了选择器变量以匹配html:

/* Application Specific Variables */
contentSelector = '#content',
$content = $(contentSelector),
contentNode = $content.get(0),
$menu = $('#nav'),
activeClass = 'active',
activeSelector = '.active',
menuChildrenSelector = 'li',

What I'm supposed to do next is where I get lost. 接下来应该做的就是我迷路的地方。 All I want to do is load in html content specific to the nav link selected. 我想要做的就是加载特定于所选导航链接的html内容。 So if I clicked "Work", I would like to load a work.html file into the content section and change the url to "mywebsite.com/work". 因此,如果我点击“工作”,我想将work.html文件加载到内容部分,并将网址更改为“mywebsite.com/work”。 To keep things easy lets say work.html and all other ajaxable content is located in the same directory. 为了让事情变得简单,可以说work.html和所有其他可共享的内容都位于同一目录中。

Any help is greatly appreciated! 任何帮助是极大的赞赏! Cheers! 干杯!

So here is a real bare bones example of how I ended up ajaxifying the website I was working on that inspired this question. 所以这里有一个真正的简单例子,说明我如何最终激活我正在研究的网站,这启发了这个问题。 Sorry for the really long delay. 对不起,真的很长时间了。 First the HTML: 首先是HTML:

    <ul id="nav">
        <li id="home-link"><a href="home" class="ajaxify" title="Home">Home</a></li>
        <li id="work-link"><a href="work" class="ajaxify" title="Work">Work</a></li>
        <li id="labs-link"><a href="labs" class="ajaxify" title="Labs">Labs</a></li>
        <li id="blog-link"><a href="blog" class="ajaxify" title="Blog">Blog</a></li>
    </ul>

    <div id="content">Home Content</div>

Next the Javascript: 接下来的Javascript:

 <script type="text/javascript">

    var directory = 'content/'; //directory of the content we want to ajax in
    var state = History.getState();

    //for when they click on an ajax link
    $('.ajaxify').on('click', function(e){
        var $this = $(this);
        var href = $this.attr('href'); // use the href value to determine what content to ajax in
        $.ajax({
            url: directory + href + '.html', // create the necessary path for our ajax request
            dataType: 'html',
            success: function(data) {
                $('#content').html(data); // place our ajaxed content into our content area
                History.pushState(null,href, href + '.html'); // change the url and add our ajax request to our history
            }
        });
        e.preventDefault(); // we don't want the anchor tag to perform its native function
    });

    //for when they hit the back button
    $(window).on('statechange', function(){
        state = History.getState(); // find out what we previously ajaxed in
        $.ajax({
            url: directory + state.title + '.html', //create our path
            dataType: 'html',
            success: function(data) {
                $('#content').html(data);
            }
        });
    });
    </script>

Essentially all I am doing is intercepting anchor tag clicks with the class 'ajaxify' used to signal what specific content I want to load in. Once I intercept the click and determine what content to load, I then use history.js pushSate() to keep track of what order the user is going through the site and change the url without reloading the page. 基本上我所做的就是用“ajaxify”类拦截锚标签点击,用于表示我想要加载的具体内容。一旦我拦截了点击并确定要加载的内容,我就使用history.js pushSate()来跟踪用户通过网站的订单,并在不重新加载页面的情况下更改网址。 If they decide to hit the back button, the statechange listener will load in the correct content. 如果他们决定点击后退按钮,状态更改侦听器将加载正确的内容。 If they decide to hit the refresh button, you will need to come up with a method of duplicating your index page with the different url names. 如果他们决定点击刷新按钮,您将需要提供一种使用不同网址名称复制索引页的方法。 This is easy to do in php or you could just copy, paste, and rename the index page as needed. 这在php中很容易,或者你可以根据需要复制,粘贴和重命名索引页面。

Here is an example I posted on Github: https://github.com/eivers88/ajaxify-simple-demo 这是我在Github上发布的一个例子: https//github.com/eivers88/ajaxify-simple-demo

Just a reminder, when working with ajax locally, it is best to run your projects on a personal web server like MAMP or WAMP . 请注意,在本地使用ajax时,最好在MAMPWAMP等个人Web服务器上运行项目。 This demo will fail in chrome without a server running. 如果没有服务器运行,此演示将在chrome中失败。 However, it should work in firefox without a server. 但是,它应该在没有服务器的Firefox中工作。

There are a variety of things you would need to do given those requirements. 鉴于这些要求,您需要做很多事情。 Here is a function you can use: 这是一个你可以使用的功能:

function doAjax(htmlpage){
$.ajax({
    url:"/"+htmlpage, 
    type: 'GET',
    success: function(data){
        $('#content').html(data)
    }
});
}

The the simplest way to hook this out would be to adjust your tags above to be like: 解决此问题的最简单方法是将上面的标记调整为:

<a href="#" onclick="doAjax('work.html');return false;"><img></a>

It would however be slightly more "correct" (though functionally the same) to do this with jquery in a script tag at the top: 然而,在顶部的脚本标记中使用jquery执行此操作会稍微“正确”(尽管功能相同):

$(function () {
    $('#buttonid1').click(function(){doAjax('work.html')});
    $('#buttonid2').click(function(){doAjax('about.html')});
    $('#buttonid3').click(function(){doAjax('contact.html')});
});

Note in both cases, these "work.html" pages shouldn't be full html documents, they should just be what goes in your content div. 请注意,在这两种情况下,这些“work.html”页面不应该是完整的html文档,它们应该只是内容div中的内容。

This ajax will do nothing to change the url that your visitor sees. 此ajax将无法更改访问者看到的网址。 To get that to work, you'll have to use the html5 history api . 为了实现这一点,你必须使用html5历史API It is quite a bit more complicated than the the ajax request I have above. 它比我上面的ajax请求复杂得多。 So if you don't have a full mastery of what I wrote above, it may or may not be appropriate. 因此,如果您没有完全掌握我上面所写的内容,那么它可能适合也可能不适合。

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

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