jquery - 哈希后将数据存储在 URI 中

标签 jquery ajax url-rewriting uri fragment-identifier

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

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

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

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

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

foo = hash.get('foo');
hash.set('bar','baz');

最佳答案

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

/**
 * 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('&');
    }
}

关于jquery - 哈希后将数据存储在 URI 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1236217/

相关文章:

ajax - 正常看到状态 204 然后状态 200?

java - Tuckey Urlrewriter 更改 JS 和 CSS 资源的 url 请求

regex - 使用正则表达式重写规则以添加正斜杠

apache - htaccess 无法很好地重写隐藏扩展名

javascript - jQuery 中的正则表达式匹配

Javascript 有条件地构建 mvc actionlink

php - 启用所有选择选项直到 selectedIndex

asp.net - 通过 jQuery AJAX 加载 ASP.Net 用户控件

javascript - 修改侧边导航,突出显示页面的哪一部分被查看

jQuery div 悬停功能在 Firefox 中不起作用