开发者

Urls for results of Ajax requests

开发者 https://www.devze.com 2022-12-17 18:35 出处:网络
Hi guys I\'ve developed a google maps application wit开发者_如何学Chaneat search feature - however I\'ve noticed that I would like to have something like what the facebook guys have done as in having

Hi guys I've developed a google maps application wit开发者_如何学Cha neat search feature - however I've noticed that I would like to have something like what the facebook guys have done as in having a hardlink to a page that is generated by an ajax query. Example you click on a link in facebook and it appends to the current url and you can copy paste the new url to get the requested page ... how do I implement something like that...


sounds like you want to update the browser's url with a link that can be used to re-visit the page in its current state. that can be done by manipulating the browser history.

a good solution for this is:

Really Simple History

http://code.google.com/p/reallysimplehistory/

Really Simple History is a lightweight JavaScript library for the management of bookmarking and browser history in Ajax/DHTML applications. RSH serializes application data in an internal JavaScript cache so that bookmarks and the back button can be used to return your application to an earlier state.

check out this excellent example:

http://www.justise.com/2009/01/26/enabling-the-back-button-in-ajax-applications/


You can use the location.hash (the part of the URL after the #) to set "hard links" from withing JavaScript. This does not cause the page to reload, like changing the location.href does, but you can fetch the value and use it in JavaScript.

Consider this example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<head>
    <title>Hash test</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <script type="text/javascript">
    window.onload = function() {
        // The substring(1) removes the # from the hash.
        var hash = window.location.hash.substring(1);

        // Use a default value if no has was passed.
        if(hash == '') {
            hash = 'Nothing';
        }

        // Use the value.
        document.getElementById('selection').innerHTML = hash;
    }

    /** Sets the hash and updates the page value */
    function setHash(newHash) {
        window.location.hash = newHash;
        document.getElementById('selection').innerHTML = newHash;
    }
    </script>
</head>
<body>
    <div>
        <div>
            <a href="javascript: void();" onclick="setHash('page1');">Page 1</a> | 
            <a href="javascript: void();" onclick="setHash('page2');">Page 2</a> |
            <a href="javascript: void();" onclick="setHash('page3');">Page 3</a>
        </div>
        <div>
            You have selected: <span id="selection">...</span>
        </div>
    </div>
</body>
</html>

When you click on one of the page links, the location.hash is changed, the URL of the browser is updated and the value used in the page is changed. If the user then copies the URL directly from the address bar, or bookmarks it, the selected page will be reloaded when the URL is requested again.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号