2009-08-05 36 views
5

Tôi muốn để lưu trữ cặp khóa/giá trị trong URI sau khi băm để sử dụng trên các mặt hàng như sau:lưu trữ dữ liệu trong các URI Sau Hash

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

Có một giải pháp tồn tại trước đó đã thực hiện điều này hoặc tôi có nên tự cuộn mình không? Tôi đã sử dụng JQuery nên một giải pháp JQuery sẽ đặc biệt được chào đón.

Ý tưởng ban đầu của tôi là sử dụng regex, nhưng điều đó trở nên phức tạp, đặc biệt khi bạn thêm cần phải thoát cả khóa và giá trị.

EDIT: Hãy để tôi làm rõ. Tôi muốn làm một việc như sau:

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

Trả lời

7

Đối với bất cứ ai quan tâm, đây là giải pháp tôi đến với: JQuery Hashchange Plugin

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

Bạn có thể lưu trữ dữ liệu JSON sau băm. Tôi đã xem xét thực hiện điều đó - nó sẽ tránh việc phân tích cú pháp, mặc dù bạn có thể mở chính mình để giả mạo.

+0

Đó là nhiều hơn một chút nặng hơn tôi muốn; cặp khóa/giá trị là tất cả những gì cần thiết. Trong trường hợp không có một lựa chọn sạch hơn, tôi có thể đi với điều này, tuy nhiên, chỉ vì lợi ích của việc có thể sử dụng một tùy chọn đóng gói sẵn. Chúng ta sẽ thấy. – Imagist

Các vấn đề liên quan