Trouble setting webview cookie with webview.evalJS function and beforeload event

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

I've been stuck on this problem for the last few days and can't seem to work out what the problem might be. I'm trying to set a cookie before loading a page in a webview on android. Code looks like this:

var container = Ti.UI.createView();
win.add(container);
 
var webview = Ti.UI.createWebView({url:SITE_PATH+'user/'+user.uid+'/edit', height:Ti.UI.FILL, width:Ti.UI.FILL}); 
var cookie = Titanium.App.Properties.getString("cookie");
webview.addEventListener('beforeload', function(e)
{   
    Titanium.API.info('calling beforeload');
    webview.evalJS('document.cookie="'+cookie+'";'); 
});
webview.addEventListener('load', function(e)
{   
    Titanium.API.info('cookie is:'+webview.evalJS("document.cookie"));
});
 
container.add(webview);
win.open({modal:true});
 
var refresh = Titanium.UI.createButton();
refresh.addEventListener('click', function()
{   
    webview.reload();
});
The weird thing is after hitting the reload button a few times the cookie kicks in and everything is ok, just can't get it to work first time ever. I am using Drupal as a back end and all I can think of is that it is possibly causing some sort of issue? Why does the beforeload function not set the cookie properly?

Any help appreciated.

— asked 8 months ago by B R
0 Comments

1 Answer

you need to set it on the load, I dont think there is a document to evaulate against when the before load event is triggered.

— answered 8 months ago by Aaron Saunders
answer permalink
2 Comments
  • I've read other posts that suggest it works. Setting on the load event is no good because the cookie is used for authentication. If there is no cookie provided with the request my backend returns an access denied error...

    — commented 8 months ago by B R

  • See http://developer.appcelerator.com/question/20971/cookies-and-webviews-again

    — commented 8 months ago by B R

Your Answer

Think you can help? Login to answer this question!