代码之家  ›  专栏  ›  技术社区  ›  Imagist

散列后在URI中存储数据

  •  5
  • Imagist  · 技术社区  · 15 年前

    我想在散列之后将键/值对存储在URI中,以便在客户端使用,如下所示:

    http://www.foo.com/index.html#foo=bar&baz=quux
    

    是否有现成的解决方案已经做到了这一点,或者我应该推出自己的解决方案?我已经在使用JQuery,因此JQuery解决方案将特别受欢迎。

    我最初的想法是使用正则表达式,但这会变得很复杂,特别是当您需要同时转义键和值时。

    编辑:让我澄清一下。我想这样做:

    foo = hash.get('foo');
    hash.set('bar','baz');
    
    3 回复  |  直到 14 年前
        1
  •  8
  •   Imagist    15 年前

    对于任何感兴趣的人,以下是我提出的解决方案:

    /**
     * Copyright 2009 by David Kerkeslager
     * Released under the BSD License (http://davidkerkeslager.com/license.txt).
     *
     * This library defines an object-literal which allows one to store key/value pairs after the hash (#) in the URI.
     * The syntax of the storage is modeled after the way that GET variables are stored after the question mark (?) in
     * the URI.
     *
     * Example URI: "http://www.foo.com/index.html#foo=bar&baz=quux"
     *
     * Note: it should be obvious that this should not be used for storing private data of any kind.
     */
    
    var URIHash =
    {
        /**
         * Dump the contents of the URI hash into an associative array. If the hash is invalid, the method returns
         * undefined.
         */
        dump : function()
        {
            var hash = document.location.hash;
            var dump = new Array();
    
            if(hash.length == 0) return dump;
    
            hash = hash.substring(1).split('&');
    
            for(var key in hash)
            {
                var pair = hash[key].split('=');
    
                if(pair.length != 2 || pair[0] in dump)
                    return undefined;
    
                // escape for storage
                dump[unescape(pair[0])] = unescape(pair[1]);
            }
    
            return dump;
        },
    
        /**
         * Takes an associative array and stores it in the URI as a hash after the # prefix, replacing any pre-
         * existing hash.
         */
        load : function(array)
        {
            var first = true;
            var hash = '';
    
            for(var key in array)
            {
                if(!first) hash += '&';
                hash += escape(key) + '=' + escape(array[key]);
            }
    
            document.location.hash = hash;
        },
    
        /**
         * Get the value of a key from the hash.  If the hash does not contain the key or the hash is invalid,
         * the function returns undefined.
         */
        get : function(key)
        {
            return this.dump()[key];
        },
    
        /**
         * Set the value of a key in the hash.  If the key does not exist, the key/value pair is added.
         */
        set : function(key,value)
        {
            var dump = this.dump();
            dump[key] = value;
    
            var hash = new Array();
    
            for(var key in dump)
                hash.push(escape(key) + '=' + escape(dump[key]));
    
            document.location.hash = hash.join('&');
        }
    }
    
        2
  •  1
  •   Jon Galloway    15 年前

    您可以在散列之后存储JSON数据。我一直在考虑这样做——这样可以避免解析,尽管你可能会被篡改。

        3
  •  0
  •   Loren    13 年前

    Ben Alman的JQuery Hashchange插件为同一页面上的多个小部件设置哈希参数。它还允许旧浏览器通过哈希更改进行备份。

    http://benalman.com/projects/jquery-hashchange-plugin/

    http://benalman.com/projects/jquery-bbq-plugin/

        4
  •  0
  •   Joe Van Leeuwen    5 年前

    今天,这一切都有可能用计算机来完成 URLSearchParams

    假设url http://www.foo.com/index.html#foo=bar&baz=quux 你能行

    const hash = window.location.hash.slice(1)
    searchParams = new URLSearchParams(hash)
    searchParams.get("foo") //=> "bar"
    searchParams.set("waldo", "fred")
    searchParams.toString() //=> "foo=bar&baz=quux&waldo=fred"