简体   繁体   中英

How to create dynamic pages using # ?

I have noticed that aa lot of pages like Twitter and a few other sites have incorporated AJAX into their design. One thing that has caught my attention is the use of #! in URLs. I am wonerding how I can do this for myself or the method they are using, Thanks!

You can start with something very simple and use either Hashchange or BBQ plugin. Read the manuals of both and you will grasp the idea.

And here is a short and general introduction: http://code.google.com/intl/en-EN/web/ajaxcrawling/docs/html-snapshot.html

UPDATE:

Well, let's take Hashchange plugin as an example. The following code is very primitive, but I think it will help to understand the basic part

HTML:

<ul>
    <li><a href="/about">About</a></li>
    <li><a href="/contact">Contact Us</a></li>
    <li><a href="/links">Links</a></li>
</ul>

<div id="page"></div>

JS:

$(function(){

    /*
     * We override the default
     * behaviour of our links
     * and change the hash of the URL,
     * e.g. '/contact' -> '#contact',
     * so the address bar of the browser
     * would change to 
     * 'http://example.com#contact'
     */
    $('ul').find('a').click(function() {
        var hash = $(this).attr('href').replace('#', '');
        window.location.hash = hash;

        return false;
    });

    /*
     * The main hashchange logic
     *
     * We use jQuery.load to retrieve
     * a specific part of the loaded document,
     * #page here
     */
    $(window).hashchange(function() {
        var newLoc = window.location.hash.replace('#', '');

        $('#page').load('/' + newLoc + ' #page');
    });

    $(window).hashchange();

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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