Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Can't access variables from a web view

Hi,

I've just started out using Titanium and it is a great tool.

Just having a bit of an issue passing a var from a button click to a webview

Here is the code that listen for a click on a button and opens the webview. You can see i set a variable called 'justtesting' to the web view (employerID is definitely set, i have double checkeded)

b1.addEventListener('click', function(e)
{


    var w = Ti.UI.createWindow();

    w.orientationModes = [
                          Titanium.UI.PORTRAIT,
                          Titanium.UI.LANDSCAPE_LEFT,
                          Titanium.UI.LANDSCAPE_RIGHT
                          ];

    var webview = Ti.UI.createWebView();

    webview.url = 'profile.htm';

    webview.justtesting = employerID;

    w.add(webview);

    Titanium.UI.currentTab.open(w,{animated:true});

});

Then i have the following HTML file. The #foo div gets it content changed, but the alert doesn't fire.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <script type="text/javascript" src="jquery-1.3.2.js"></script>

</head>
<body id="mybody" style="height:500px;background-color:#999">

    Hello from local webview. You should see an indicator in the middle and my text should be blue.
    We are also including JQuery
    <div id="foo" style="margin-top:20px;font-weight:bold">hello</div>

    <script>

$('#foo').html('i have changed');

alert(Ti.UI.currentWindow.justtesting);

    </script>


</body>
</html>

Any ideas

— asked March 16th 2010 by Mark Beech
  • mobile
  • passing
  • variables
  • webview
0 Comments

6 Answers

  • Accepted Answer

    i solved this by firing an event when the webview is finished loading.

    so, in your parent js file:

    var webview1 = Titanium.UI.createWebView({url:'somePage.html'});
    webview1.addEventListener('load', function() {
        Ti.App.fireEvent('pageReady',{id:1});
    });
    Ti.UI.currentWindow.add(webview1);
    

    then you can then listen for this event inside the web view and get the variables sent along with the event

    <script type="text/javascript">
    Ti.App.addEventListener('pageReady',function(e)
    {
        alert('the id is' + e.id);
    }
    
    — answered March 17th 2010 by Andrew Harrison
    permalink
    2 Comments
    • This is working only while passing basic data like numbers, strings etc. but not for passing Titanium element objects like VideoPlayer.

      If I'm trying to send the Titanium VideoPlayer object then the control is not yet all going to the corresponding event handler function, my code blocks are specified below.

      parent JS code:

      var videoPlayer = Titanium.Media.createVideoPlayer({
        url: MY_VIDEO_URL,
        backgroundColor: '#000'
      });
      
      webView.addEventListener('load', function() {
        Ti.App.fireEvent('getPlayer', {player: videoPlayer}); 
      });
      

      WebView template JS code:

      Ti.App.addEventListener('getPlayer', function(e) {
        Ti.API.info("getPlayer event handler...");
        Ti.API.info(e);
      });
      

      Can you please help me to send Titanium objects to the webview JS.

      Thanks,
      Siva

      — commented September 24th 2011 by siva veeranki
    • I tried same please look at my code

      var mainDashboardWebView = Ti.UI.createWebView();

      mainDashboardWebView.url =mainDashboard.html;

      mainDashboardView.add( mainDashboardWebView );

      mainDashboardWebView.addEventListener('load', function() {
      Ti.App.fireEvent('pageReady',{id:1});
      });

      //web view
      <script type="text/javascript">
      Ti.App.addEventListener('pageReady',function(e)
      {
      alert('the id is' + e.id);
      }
      </script>

      No alert // :(

      — commented February 8th 2012 by Gaurav Sood
  • i solved this by firing an event when the webview is finished loading.

    so, in your parent js file:


    var webview1 = Titanium.UI.createWebView({url:'somePage.html'});
    webview1.addEventListener('load', function() {
    Ti.App.fireEvent('pageReady',{id:1});
    });

    Ti.UI.currentWindow.add(webview1);

    then you can then listen for this event inside the web view and get the variables sent along with the event


    <script type="text/javascript">
    <!–
    Ti.App.addEventListener('pageReady',function(e)
    {
    alert('the id is' + e.id);

    }

    — answered March 17th 2010 by Andrew Harrison
    permalink
    0 Comments
  • i solved this by firing an event when the webview is finished loading.

    so, in your parent js file:


    var webview1 = Titanium.UI.createWebView({url:'somePage.html'});
    webview1.addEventListener('load', function() {
    Ti.App.fireEvent('pageReady',{id:1});
    });

    Ti.UI.currentWindow.add(webview1);

    then you can then listen for this event inside the web view and get the variables sent along with the event


    <script type="text/javascript">
    <!–
    Ti.App.addEventListener('pageReady',function(e)
    {
    alert('the id is' + e.id);

    }

    — answered March 17th 2010 by Andrew Harrison
    permalink
    0 Comments
  • You might want to try attaching 'justtesting' to your window object rather than the webview object.

    w.justtesting = employerID;

    Worth noting that the alert in your HTML is attempting to indicate exactly that.

    — answered March 16th 2010 by Steve Mockensturm
    permalink
    0 Comments
  • i Tried the same thing

    var mainDashboardWebView = Ti.UI.createWebView();

    mainDashboardWebView.url =mainDashboard.html;

    mainDashboardView.add( mainDashboardWebView );

    mainDashboardWebView.addEventListener('load', function() { Ti.App.fireEvent('pageReady',{id:1}); });

    //web view <script type="text/javascript"> Ti.App.addEventListener('pageReady',function(e) { alert('the id is' + e.id); } </script>
    No alert // :(

    — answered February 8th 2012 by Gaurav Sood
    permalink
    0 Comments
  • As of fully updated Titanium 20th July 2013, the above code does not work - well, it sort of does… If I attempt to do anything with the variable in the web view, for example, display it in an alert , the alert displays with the correct information in it, but then the system hangs up and I can't clear the alert box.
    I have attempted several methods of sending variables to a webview, but, they all demonstrate the 'hang' as does the Titanium 2 demo code at https://wiki.appcelerator.org/display/guides/Communication+Between+WebViews+and+Titanium - I would expect that, but i have not found a working Titanium three method yet.
    Please help ! Thanks

    — answered July 20th 2013 by Simon Anthony
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.