簡體   English   中英

要求Ajax請求的結果

[英]Urls for results of Ajax requests

嗨,大家好,我開發了一個具有整潔的搜索功能的google maps應用程序-但是,我注意到我想要做一個像Facebook的家伙那樣做的事情,就像硬鏈接到ajax查詢生成的頁面一樣。 例如,您單擊facebook中的鏈接,並將其附加到當前URL,然后可以復制粘貼新的URL以獲取所請求的頁面...我該如何實現類似的功能...

聽起來好像您想使用鏈接來更新瀏覽器的url,該鏈接可用於以當前狀態重新訪問該頁面。 可以通過操縱瀏覽器歷史記錄來完成。

一個好的解決方案是:

真正簡單的歷史

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

Really Simple History是一個輕量級的JavaScript庫,用於管理Ajax / DHTML應用程序中的書簽和瀏覽器歷史記錄。 RSH在內部JavaScript緩存中序列化應用程序數據,以便可以使用書簽和后退按鈕使您的應用程序返回到較早的狀態。

看看這個出色的例子:

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

您可以使用location.hash (#后面的URL部分)來設置JavaScript的“硬鏈接”。 這不會像更改location.href那樣導致重新加載頁面,但是您可以獲取值並在JavaScript中使用它。

考慮以下示例:

<!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>

當您單擊頁面鏈接之一時, location.hash會更改,瀏覽器的URL將更新,並且頁面中使用的值也會更改。 如果用戶然后直接從地址欄中復制URL或將其添加為書簽,則再次請求URL時將重新加載所選頁面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM