- //把json格式转化成url的编码方式
- // e.g. Ext.urlEncode({foo: 1, bar: 2}); would return "foo=1&bar=2"
- urlEncode: function(o, pre){//o为json对象,pre为前缀
- var undef, buf = [], key, e = encodeURIComponent;
- for(key in o){
- undef = !Ext.isDefined(o[key]);
- Ext.each(undef ? key : o[key], function(val, i){
- buf.push("&", e(key), "=", (val != key || !undef) ? e(val) : "");
- });
- }
- if(!pre){
- buf.shift();
- pre = "";
- }
- return pre + buf.join('');
- },
- //把URL字符串编码转化成json对象
- //Ext.urlDecode("foo=1&bar=2"); // returns {foo: "1", bar: "2"}
- //Ext.urlDecode("foo=1&bar=2&bar=3&bar=4", false); // returns {foo: "1", bar: ["2", "3", "4"]}
- urlDecode : function(string, overwrite){//string URL参数,overwrite针对同一参数的多个值是否覆盖,false为不覆盖,那就返回这个参数的数组
- var obj = {},
- pairs = string.split('&'),
- d = decodeURIComponent,
- name,
- value;
- Ext.each(pairs, function(pair) {
- pair = pair.split('=');
- name = d(pair[0]);
- value = d(pair[1]);
- obj[name] = overwrite || !obj[name] ? value :
- [].concat(obj[name]).concat(value);
- });
- return obj;
- }
原文链接: http://lovebeyond.iteye.com/blog/949502