0

I want to grab the variables contained in the url after somebody logs into my Facebook app.

For example:

// window.location = .../#access_token=CTTG4fT3ci...&expires_in=5298
hash = window.location.hash;
data = PARSE(hash);
console.log(data['access_token'] + ', ' + data['expires_in']);
// returns: CAAG4fT3ci..., 5298

Is there a method or function similar to JSON.parse() that would convert "hash" into an array or object?

4

1 回答 1

0
function PARSE (hash) {
    var l, chunk, i = 0, out = {};
    var chunks = hash.substr(1).split('&');
    for ( l = chunks.length; i < l; i++ ) {
        chunk = chunks[i].split('=');
        out[ chunk[0] ] = chunk[1];
    }
    return out;
}

这是小提琴:http: //jsfiddle.net/VwLJS/

于 2013-08-23T21:12:24.180 回答