开发者

How do I deserialize a big long querystring in jQuery?

I'm trying to figure out how to enable the back and forward buttons in my AJAX application, which is dynamic and database driven. To capture the state, I am trapping some values开发者_Go百科 for url, type, and data in an associative array.

I am getting the hash string back from the browser during the 'hashchange' event, I just don't know how to turn it back into an associative array from the encoded query string. Can anybody help?


something like this?

var hash = 'one=1&two=2',
    split = hash.split('&');

var obj = {};
for(var i = 0; i < split.length; i++){
    var kv = split[i].split('=');
    obj[kv[0]] = decodeURIComponent(kv[1] ? kv[1].replace(/\+/g, ' ') : kv[1]);
}
console.log(obj);


jQuery has a deserialize plugin that might fit the bill.

Alternatively take a look at this SO answer which writes a jQuery function to do the same.


function getQuery() {
    var query = window.location.hash.substring(1);
    var queryArray = [];
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        queryArray.push([pair[0], pair[1]);
    }
    return queryArray;
}


Thanks guys. It looks like a call to $.bbq.getState() does most of it for me. But I will need that deserialize method later...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜