URLs for Ajax Request Results

Hi guys. I developed a Google Maps app with a neat search feature. However, I noticed that I would like to have something similar to what the guys from facebook did, as if there is a hard link to the page that the ajax request is being created. For example, you click on the link in facebook and join the current URL, and you can copy the paste of the new URL to get the requested page ... how can I implement something like this ...

0
source share
2 answers

You can use location.hash(the part of the URL after #) to set up "hard links" using JavaScript. This does not reload the page as a change location.href, but you can get the value and use it in JavaScript.

Consider the following 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, it changes location.hash, the browser URL is updated and the value used on the page changes. If the user then copies the URL directly from the address bar or bookmarks, the selected page will reload when the URL is requested again.

+1
source

Source: https://habr.com/ru/post/1730482/


All Articles