webview communication error

You must Login before you can answer or comment on any questions.

Recieving error unexpected token var when calling evalJS

var downloadswebview = Ti.UI.createWebView(); downloadswebview.scalesPageToFit = false; downloadswebview.addEventListener('load',function(e) { downloadswebview.evalJS( "var links = document.getElementsByName('downloadlink');for (link in links){links[link].setAttribute('onclick','totifromwebview(this.href)')}function totifromwebview( clickedurl ){ Ti.App.fireEvent('detailWebview', clickedurl ); }" ) }); Ti.App.addEventListener('detailWebview', function(e) { alert('Link clicked!'); Ti.API.info(e.value);});

3 Answers

before calling "evalJS", you need to load some html into the webview and call evalJS inside "load" event of the webview.

html is irrelevant, can be something like this:

<html>
<body />
</html>

— answered 1 year ago by yury bogomo
answer permalink
3 Comments
  • it is already loaded with appengine site spllrcomdownloads.appspot.com.

    — commented 1 year ago by David Jackson

  • if the content of the webview is coming from a remote server, Titanium framework is not being injected there.

    do the

    alert('titanium: ' + Ti);
    you should see 'undefined'

    — commented 1 year ago by yury bogomo

  • Web page is downloaded to device first and saved to a file in application data directory then it's loaded into webview so it's not a remote file!

    — commented 1 year ago by David Jackson

can you post a complete code fragment to avoid further "peace-meal"ing?

one general observation:

never use "global" variables in your event handlers (one of the titanium best practices "must not to" things).

in your code, inside "load" handler, try to change downloadswebview.evalJS(...) to e.source.evalJS(...)

Your Answer

Think you can help? Login to answer this question!