Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-1779] Android: Cannot set Webview's html property after navigating somewhere

GitHub Issuen/a
TypeBug
PriorityLow
StatusClosed
ResolutionWon't Fix
Resolution Date2017-08-08T18:48:13.000+0000
Affected Version/sRelease 3.0.0
Fix Version/sn/a
ComponentsAndroid
Labelsapi
ReporterBill Dawson
AssigneeIngo Muschenetz
Created2011-04-15T03:02:02.000+0000
Updated2017-08-08T18:48:13.000+0000

Description

Problem

When a web view on Android navigates to an external URL, the "html" property becomes unusable -- you cannot get or set its value. If you set the "html" property, then navigate to an external URL, setting the "html" property has no effect on the web view. And reading the "html" property reflects what you originally set it as before navigating to the external URL.

Failcase

The following code demonstrates setting the "html" property, with a link to an external site.

var win = Titanium.UI.createWindow({
    backgroundColor:'#fff'
});

var html1 = '<html><body><a href="http://www.google.com">Google</a></body></html>';
var html2 = '<html><body><a href="http://www.yahoo.com">Yahoo</a></body></html>';

var wv = Ti.UI.createWebView({html: html1, backgroundColor: 'yellow', top: 50});
win.add(wv);

var btn = Ti.UI.createButton({
    title: 'Set HTML', top: 10, height: 40, width: 100, left: 10
});
btn.addEventListener('click', function() {
    alert(wv.html);
    wv.html = html2;
});
win.add(btn);

win.open();

Steps to Reproduce with Failcase

  1. Start off by clicking "Set HTML", and it will alert "web.html" for you to see; the web view will update to contain a link to Yahoo. So far so good.
  2. Click the link to Yahoo,
  3. Then click "Set HTML" again, and the web view will not update. Also, the alerted html will be from what you previously set.

If you run this same code on iOS, you'll see Yahoo's HTML alerted, and then the web view will properly update to be the html you specified.

Expected Results

A new page shows with just one link (to yahoo this time.)

Workaround

Two workarounds are required, but both are rather simple:

To get around the setting-doesn't-update issue, first set the url property of the web view to an empty string. In the failcase, this would look like this:

btn.addEventListener('click', function() {
    alert(wv.html);
    wv.url = '';
    wv.html = html2;
});

Now getting around the reading-html-returns-stale-html issue is a bit more difficult. We need to run some JavaScript to do so. This isn't entirely foolproof, but it should give you a fairly accurate dump of the HTML after the page loads:

btn.addEventListener('click', function() {
    var response = '<html>' + web.evalJS('document.getElementsByTagName("html")[0].innerHTML') + '</html>';
    alert(response);
    wv.url = '';
    wv.html = html2;
});

Tested On

Titanium SDK version: 1.6.0 (02/23/11 12:34 9882e81)
BROKEN on Android Emulator 2.2 APIs
BROKEN on Android Device Epic 4G 2.2
WORKS on iPhone Simulator 4.2

Associated Helpdesk Ticket

http://developer.appcelerator.com/helpdesk/view/76225">http://developer.appcelerator.com/helpdesk/view/76225

Comments

  1. Dawson Toth 2011-04-15

    I'm seeing the same behavior on the other side of things -- getting the HTML, as opposed to setting it. I'll update the original ticket with additional details.

  2. Junaid Younus 2012-08-16

    Tested using TiSDK 2.2.0v20120816015712 on a Samsung Galaxy S2, issue still valid.
  3. Lee Morris 2017-08-08

    Closing due to inactivity. If this issue still exists, please raise a new ticket.

JSON Source