我通过注入(inject)的 javascript(又名 inappbrowser 回调)在打开的浏览器中打开并执行此函数。
该功能有效,因为我看到了警报。 inappbrowser 通过 window.open(...) 打开:
var f_el_tname = document.body.getElementsByTagName("the_tag")[0];
//the above alerted "undefined" in android browser and the correct value in the desktop
//rewriting variable for debug purposes
f_el_tname = document.body.getElementsByTagName("the_tag");
alert(f_el_tname.length); //this gives "0" in phonegap android browser and "1" in desktop (correct)
for(var i = 0; i < f_el_tname.size; i++){
alert(f_el_tname); //this does not even run
}
为什么会发生这种情况?对于“桌面”和“安卓”,我指的是在桌面或安卓中访问 phonegap 实例,因此代码和上下文几乎相同。知道为什么吗?
编辑:
我认为这可能是因为 document.body.getElementsByTagName("the_tag");
中的 document 指的是应用程序文档而不是 inappbrowser 中的文档. 如何在 loadstop 回调中获取浏览器中的文档?
窗口由 var ref = window.open(...);
打开
编辑 2:根据要求,这是代码
var ref = window.open(url,'_blank','location=yes,toolbar=no,hidden=yes','closebuttoncaption=Return');
ref.addEventListener('loadstop', function(){
var f_el_tname = ref.document.body.getElementById("l_fdb");
//the above gives an error
});
最佳答案
尝试使用 inappbrowser.executeScript 来做到这一点:
var ref = cordova.InAppBrowser.open(url,'_blank','location=yes,toolbar=no,hidden=yes');
ref.addEventListener('loadstop', function() {
var code = '(function(){ return document.getElementById("l_fdb"); })()';
ref.executeScript({code: code}, function(results) {
console.log('l_fdb: ' + results);
});
});
executeScript
用法示例可以在 the plugin tests 中找到.
关于javascript - 无法在 inAppBrowser 内的 phonegap 中捕获 "document.(...).tagName",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36158210/