7
votes

I open and execute this function inside the opened browser via injected javascript (aka inappbrowser callbacks).

The function works because I see the alerts. The inappbrowser is opened via 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
}

Why exactly is this happening? With "desktop" and "android" I'm referring to accessing the phonegap instance in the desktop or in android, so the code and context are virtually the same. Any idea on why?

EDIT :

I think this might be happening because document in document.body.getElementsByTagName("the_tag"); is referring to the app document and not the document inside the inappbrowser. How can I get the document inside the browser inside the loadstop callback?

The windows is opened by var ref = window.open(...);

EDIT 2: as requested, here is the code

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

});
1
Is this a typo i < f_el_tname.size -> i < f_el_tname.length? document in the inappbrowser.executeScript context should mean the document inside the inappbrowser.daserge
@daserge but that seems to be injected code iab.executeScript. I am only opening the callback and calling document directly in the callback function. Do I need to inject the script? And if so how can I get info from the new document to phonegap?Fane
Can you show the code how you are doing this: I open and execute this function inside the opened browser via injected javascript (aka inappbrowser callbacks).? See github.com/apache/cordova-plugin-inappbrowser/… for details on how to get results back from iab.daserge
Can you try to call ref.executeScript({code: code}, function(results){...});? var code = '(function(){\n' + ' return document.body.getElementById("l_fdb");\n' + '})()';daserge
@daserge but I don't understand, how am I supposed to return information from the inappbrowser back to my app, like that? (Please enter an answer so I can understand your code)Fane

1 Answers

4
votes

Try to do it using 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);
    });
});

Examples of executeScript usage can be found in the plugin tests.