0
votes

I like to add innerhtml content(Just Anchor Link) when an external website is opened.

Below code is used to open the external site in inAppBrowser. var ref = window.open(url, '_blank', 'location=yes');

I tried with below code to add the innerhtml but its not adding the content to the opened website. Can you please suggest an solution?

ref.addEventListener('loadstop', function() {
  //Page loaded! some code here..
  ref.executeScript({
    code: "var evaluateFeedback =   
    function() {
    return 'Done';
  };
  "},

    function(data) {
    ref.document.body.innerHTML =
      "<b>Hello, stackoverflow!   < /
    b > ";

  }
  );
});
1

1 Answers

0
votes

ref is a reference to the inAppBrowser object, you can't do ref.document.body.innerHTML because the inAppBrowser object doesn't have a document property

You are using executeScript in the wrong way, the code you want to inject is the code on the code param.

If you do it this way it will work:

var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes,toolbarposition=top');
ref.addEventListener('loadstop', function() {
    ref.executeScript({code: "document.body.innerHTML = '<b>Hello, stackoverflow!</b>';"});
});

But that will replace your website content with just the Hello, stackoverflow! message, if you want to append it, you should use

var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes,toolbarposition=top');
ref.addEventListener('loadstop', function() {
    ref.executeScript({code: "document.body.innerHTML = document.body.innerHTML +'<b>Hello, stackoverflow!</b>';"});
});