用于Ajax请求结果的URL

时间:2010-01-24 08:51:37

标签: php javascript ajax

嗨,大家好我已经开发了一个具有整洁搜索功能的谷歌地图应用程序 - 但是我注意到我想要像facebook那样做的东西,比如有一个由一个页面生成的硬链接ajax查询。例如,您点击Facebook中的链接并将其附加到当前网址,您可以复制粘贴新网址以获取请求的网页...如何实现类似的内容...

2 个答案:

答案 0 :(得分:2)

听起来您想要使用可用于在当前状态下重新访问该页面的链接来更新浏览器的URL。这可以通过操纵浏览器历史来完成。

一个很好的解决方案是:

真正简单的历史

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

  

真正简单的历史是轻量级的   用于管理的JavaScript库   书签和浏览器历史记录   Ajax / DHTML应用程序。 RSH   序列化应用程序数据   内部JavaScript缓存这样   书签和后退按钮都可以   用于将您的申请退回到   早先的州。

看看这个很好的例子:

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

答案 1 :(得分:1)

您可以使用location.hash (#后面的网址部分)来设置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时将重新加载所选页面。

相关问题