Titanium JIRA Archive
Appcelerator Community (AC)

[AC-1800] 'window' JS element not available to EvalJS on Android WebView, SDK 3.1.0

GitHub Issuen/a
TypeBug
Priorityn/a
StatusClosed
ResolutionCannot Reproduce
Resolution Date2014-01-02T06:30:58.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsTitanium SDK & CLI
Labelsandroid, sdk3, webview
ReporterPaul Eustice
AssigneeRitu Agrawal
Created2013-04-30T15:56:18.000+0000
Updated2016-03-08T07:40:41.000+0000

Description

I am trying to run some JS inside of a webview on Android that fires events within my hybrid app. When using SDK 3.0.2 the evalJS runs fine, but switching to 3.1 (necessary for the persistent Android webview localStorage bugfix) stops it from working.
I believe it is caused by the 'window' JS object not being available.

Code extract from Titanium JS file:

API.fireEvent = function(name, data){
    if (data) {
        var js = 'window.fireCustomEvent("' + name + '", ' + JSON.stringify(data) + ');';
    }else{
        var js = 'window.fireCustomEvent("' + name + '");';
    }
    
    webView.evalJS(js);

    log("Call EvalJS on Webview: " + js);
    
};

var locale = Ti.Locale.getCurrentLocale();
API.fireEvent('returnLocale', { locale: locale } );

Code extract from webview JS file:

window.fireCustomEvent = function(name, data){
 alert("Fire custom event " + name);
}

Comments

  1. Carter Lathrop 2013-04-30

    Hello Paul, When submitting a bug report it is important that you provide a working test case that is able to be easily pasted into an app.js and when run, clearly shows the problem. This ensures a speedy review and thorough understanding of the problem on both sides. Would you mind editing the bug description to provide this test case? For future reference: https://wiki.appcelerator.org/display/guides/How+to+Submit+a+Bug+Report. A simple app.js that replicates the issue will suffice. Also be sure to wrap your code in {.code.} tags to ensure persistent formatting. (remove the dots) Thank you for your cooperation, Carter
  2. Paul Eustice 2013-05-01

    Hi Carter, Thanks for the reply, and sorry for poor issue-raising etiquette. I've investigated further and my initial suspicion about it being caused by a lack of the 'window' js object wasn't correct. Distilling my code down to something simple enough to demonstrate the bug (it's a large and complex hybrid app) doesn't bare fruit at the moment unfortunately. The bug definitely occurs only on 3.1.0 and not 3.0.2 but at the moment I can't reduce it down to be of help. If I find out anything more I'll update this issue.
  3. Shak Hossain 2013-12-09

    We have not heard back from you since your last update in May. Not sure if this is still throwing error in your app. If so, please send us details so that we can take next necessary step to fix. If we don't hear back, we will assume the issue is no longer valid and close it.
  4. Mostafizur Rahman 2013-12-30

    We have not been able to reproduce this issue with Titanium 3.2.0 GA SDK. If you are still running into the reported error, please let us know your test case. We sure will try to help. If we don't hear back, we will assume the issue has been resolved for you and the ticket can be closed.

    Test Environment:

    MAC OS X 10.8.5 Ti SDK 3.2.0.GA IOS Simulator 7.0.3 Ti CLI 3.2.0 Android 2.3.3, 4.2.2

    Test Code

    app.js
       var win = Titanium.UI.createWindow({
       	backgroundColor : '#fff'
       });
       
       var webview = Ti.UI.createWebView({
       	url : 'test.html'
       });
       
       var doc1 = "Hello world";
       
       webview.addEventListener('beforeload', function(e) {
       	webview.evalJS("var message='" + doc1 + "';");
       
       });
       
       win.add(webview);
       
       win.open();
       
       
    test.js
       <html>
       	<head>
       
       	</head>
       	<body>
       
       		<p id="demo">
       			Click this button
       		</p>
       
       		<button onclick="myFunction()">
       			Click
       		</button>
       
       		<script>
       			function myFunction() {
       				window.name = message;
       				var x = "The Passing value is: " + window.name;
       				document.getElementById("demo").innerHTML = x;
       			}
       		</script>
       	</body>
       </html>
       
    Thanks

JSON Source