对于使用 facebook,我这样做:
function facebookLogin() {
ref = window.open('https://www.facebook.com/dialog/oauth?client_id=YOURID&redirect_uri=https://www.facebook.com/connect/login_success.html&response_type=token', '_blank');
ref.addEventListener('loadstart', function(event){ manageLoadstart(event)});}
和
function manageLoadstart(event) {
if (event.url.indexOf('https://www.facebook.com/connect/login_success.html#') !== -1) {
var charIndex = event.url.indexOf('&');
var firstExtraction = event.url.substring(charIndex);
var newUrl = event.url.replace(firstExtraction, '');
var startUrl = newUrl.substr(0, 65);
var accessToken = newUrl.replace(startUrl, '');
saveFacebookToken(accessToken);
ref.close();
}}
我将它与 phonegap 2.6.0 一起使用。loadstop 事件不起作用。所以它会在 manageloadstart 中运行 3 次,并且每次都检查 url。