Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-10837] Android: webView throws on evalJS when string starts with `var`

GitHub Issuen/a
TypeBug
PriorityMedium
StatusReopened
ResolutionUnresolved
Affected Version/sRelease 2.1.2
Fix Version/s2014 Sprint 07, 2014 Sprint 07 SDK, Release 3.3.0
ComponentsAndroid
Labelscore, module_webview, qe-3.4.1, qe-closed-3.3.0, qe-testadded
ReporterSindre Sorhus
AssigneeUnknown
Created2012-09-09T14:56:25.000+0000
Updated2018-02-28T20:03:39.000+0000

Description

Problem

When trying to evalJS a string with code that starts with var it doesn't work and I get this error message in Console:
[INFO][TiWebChromeClient.console( 1229)] (main) [493,23278] Uncaught SyntaxError: Unexpected token var (1:http://www.google.com/)

Expected Behavior

Should not throw and instead replace the site contents with "test". Works on iOS.

Test case

var win = Ti.UI.createWindow();

var webView = Ti.UI.createWebView({
	url: 'http://google.com',
	top: 0,
	bottom: 0,
	height: Ti.UI.FILL,
});

win.add(webView);

win.open({
     modal: true,
     animated: false
});

webView.addEventListener('load', function() {
	webView.evalJS('var a="test";document.body.innerHTML=a'); // doesn't works
	//webView.evalJS('document.body.innerHTML="test"'); // works
});

Comments

  1. Sindre Sorhus 2013-03-01

    bump
  2. Sindre Sorhus 2013-08-19

    bump
  3. Sunila 2014-01-11

    If passed in value is not an expression but a statement block, extract the last statement and add prepend 'return'. https://github.com/appcelerator/titanium_mobile/pull/5210
  4. Pragya Rastogi 2014-04-28

    Verified the fix using: OSX: 10.9.2 Xcode: 5.1.1 Studio - 3.3.0.201404251359 SDK - 3.3.0.v20140425191906 acs-1.0.14 alloy-1.4.0-dev npm-1.3.2 titanium-3.3.0-dev titanium-code-processor-1.1.1-beta1 Device: LG-P970 (4.0.4) & ipod touch 2 (7.1)
  5. Ingo Muschenetz 2014-09-07

    Based on the regressions caused by this issue, reopening. We need to find a solution with less side effects.

JSON Source