Webview - declare a variable that has been passed into webview

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

I am wanting to declare a var using data passed into the webview from TI.

.html file

Ti.App.addEventListener('ad_id',function(e)
{
    var url_item = e.id;
})
Then later down the html.
<script type='text/javascript'>
document.write('<div onclick="Ti.App.fireEvent(\'openURL\', { url: \'' + url_item + '\'}); return false;"></div>');
</script>
How do i get the variable to pass to another function or a later call down the document?

1 Answer

Accepted Answer

You're code doesn't seem to make a lot of sense. You've got 2 events that have nothing to do with eachother and Im not sure if you're trying to set the var within the context of the webview or within the context of your Ti app. But if you want to try and get things set inside your already existing webview, you might try using the evalJS method on the webview. See the docs here: http://docs.appcelerator.com/titanium/2.1/index.html#!/api/Titanium.UI.WebView-method-evalJS This will allow you to execute some code, based on some value that you've already got in your Ti context.

— answered 9 months ago by Anthony Decena
answer permalink
4 Comments
  • Please ignore the last event. Only know that i am calling an event to bring a var into a webview. Once i bring the Ti. declared var into the webview, i want the ability to call that var anywhere in the webview. This where i seem to lose it. I will take a look at evalJS and see where that gets me. Thank you for your response!

    — commented 9 months ago by dan rivers

  • You can try something like this.

    In your html:

    var foo = 0;
    function setFoo(_val) {
      foo = _val || 0;
    }
    In your app:
    webview.evalJS("setFoo(6);");
    Now back in your html you use foo anytime, but if you use it after you've called the evalJS, then the value for foo will be updated. Does that make sense?

    — commented 9 months ago by Anthony Decena

  • I think this is what im wanting, but im unable to pass variables such as... App.js

    var = example;
    or
    var = 'Hello World';
    I am able to pass numbers like you did above.

    — commented 9 months ago by dan rivers

  • Show 1 more comment

Your Answer

Think you can help? Login to answer this question!