AddEventListener可在浏览器上使用,但不能在Android上使用 [英] AddEventListener works on the browser but does not on Android
问题描述
我正在使用postMessage()
函数将消息从移动应用填充到网站字段.我在浏览器上对其进行了测试,并且一切正常.但是,当我尝试在Android手机上进行测试时,没有填充任何内容.
I'm using postMessage()
function to populate a message from a mobile App to a website fields. I tested it on the browser, and everything is working perfectly. However, when I tried to test on Android phone, nothing is populated.
应用程序一侧:
var password = localStorage.getItem("password");
var language = localStorage.getItem("lang");
var credentials = username+'-'+password;
var msg = language+':'+username+'-'+password;
var loginWindow = window.open('https://link', '_blank', 'location=yes');
function giveMeUsername(){
loginWindow.postMessage(msg, 'https://link');
console.log('open window');
}
window.addEventListener('message', function(event) {
alert('listener alert');
var message = event.data;
if(message == 'giveMeUsername'){
console.log('call giveMeUsernam');
giveMeUsername();
}
}, false);
此行alert('listener alert');
在浏览器上显示警报,但是当我在Android设备(apk文件)上对其进行测试时,什么也没有显示,并且没有消息发送到网站.
This line alert('listener alert');
shows alert on the browser, but when I test it on Android device (apk file), nothing is showing, and no message gets sent to the website.
网站方面:
addScriptDeclaration("
console.log('message received1: ');
window.onload = function(){
window.opener.postMessage('giveMeUsername', '*');
console.log('message received2: ');
};
function GetSubstringIndex(str, substring, n) {
var times = 0, index = null;
while (times < n && index !== -1) {
index = str.indexOf(substring, index+1);
times++;
}
return index;
}
window.addEventListener('message', function(event) {
console.log('message received3: ');
//if(event.origin !== 'http://appery.io') return;
var message = event.data;
console.log('message received5: '+message);
//var username = message.substr(0, message.indexOf('-'));
var lang = message.slice(0,2);
var username = message.substring(message.lastIndexOf(':')+1,message.lastIndexOf('-'));
var password = message.substr(message.indexOf('-') + 1);
jQuery('input#username').val(username);
jQuery('input#password').val(password);
document.getElementById('loginbtn').click();
var x = document.getElementById('loginbtn').name;
console.log('message received6: '+ x);
}, false);
");
$document->addScriptDeclaration("
console.log('message received6: ');
");
?>
我很困惑,因为我不知道任何.apk调试方法.我只在浏览器上进行开发,测试和调试,而浏览器上的所有功能通常都可以在Android上使用.
I'm baffled as I am not aware of any .apk debugging method. I only develop, test and debug on the browser, and whatever works on the browser usually works on Android.
推荐答案
两件事:
在设备(手机)上,不支持Alert()
功能.您必须使用Cordova对话框插件. org.apache.cordova.dialogs
On a device (handset), the Alert()
function isn't supported. You must use the Cordova dialogs plugin. org.apache.cordova.dialogs
然后我尝试document.addEventListener
看看是否可以触发.
And I'd try document.addEventListener
and see if that fires.
这篇关于AddEventListener可在浏览器上使用,但不能在Android上使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!