要求获得Ajax请求的结果 [英] Urls for results of Ajax requests

查看:72
本文介绍了要求获得Ajax请求的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我开发了一种具有整洁搜索功能的google maps应用程序-但是,我注意到我想像Facebook的家伙那样做,就像对由生成的网页进行硬链接一样Ajax查询.例如,您单击facebook中的链接,该链接将附加到当前URL,然后您可以复制粘贴新的URL以获取请求的页面...我该如何实现类似的功能...

Hi guys I've developed a google maps application witha 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...

推荐答案

您可以使用 location.hash (网址中#后面的部分)进行设置JavaScript带来的硬链接".这不会像更改 location.href 那样导致页面重新加载,但是您可以获取该值并在JavaScript中使用它.

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.

请考虑以下示例:

<!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时将重新加载所选页面.

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.

这篇关于要求获得Ajax请求的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆