Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-12095] Android: EvalJS does not work before a webview is loaded

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionWon't Fix
Resolution Date2012-12-14T00:54:11.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsAndroid
Labelsn/a
ReporterAllen Yeung
AssigneeIngo Muschenetz
Created2012-12-14T00:41:52.000+0000
Updated2018-03-02T01:37:49.000+0000

Description

Example:
var win = Ti.UI.createWindow({
	
});
var webview = Ti.UI.createWebView({
	url: 'test.html'
});

webview.evalJS('var phrases = {}');
webview.evalJS('phrases.breakfast = "' + L('breakfast') + '"');
webview.evalJS('phrases.nutritional_values = "' + L('nutritional_values') + '"');

win.add(webview);

win.open();
test.html file:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title>local webview</title>
</head>
<body id="mybody" style="height:500px;background-color:#999;background-image:url(../images/bg.png)">
	This is a test
	<script>
		Titanium.API.info("--------------------------------");
		Titanium.API.info(phrases.breakfast);
	</script>

	
</body>
</html>
Expected result: You should see the value 'breakfast' printed out as it does in iOS Actual result: The value is undefined and nothing prints out.

Comments

  1. Allen Yeung 2012-12-14

    This can't be fixed in android since we rely on the webview to run evalJS. We use loadURL('javascript:') to run the javascript code. If we do this before we actually load the webpage, it won't work since the previous javascript context will be wiped out once the actual webpage loads.
  2. Eric Merriman 2013-01-10

    Closing based on Allen's comments.

JSON Source