最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to set a Javascript-HTML variable from a Android code - Stack Overflow

programmeradmin3浏览0评论

I am trying to set the value of a global variable from Android code.

               mWebView.loadUrl("javascript:(function() { " +  
                       "loginID = '"+nicu_app.urlSelected+"'; " +  
                       "})()");  

but when I load the webpage in a webview and check to see if this variable has been set by the Android code, I don't see any positive results.

Any help will be appreciated.

Thanks, Sana.

I am trying to set the value of a global variable from Android code.

               mWebView.loadUrl("javascript:(function() { " +  
                       "loginID = '"+nicu_app.urlSelected+"'; " +  
                       "})()");  

but when I load the webpage in a webview and check to see if this variable has been set by the Android code, I don't see any positive results.

Any help will be appreciated.

Thanks, Sana.

Share Improve this question asked Jul 26, 2010 at 18:55 SanaSana 9,92516 gold badges60 silver badges87 bronze badges 1
  • have you defined a javascript interface? – Jorgesys Commented Jul 26, 2010 at 19:39
Add a ment  | 

2 Answers 2

Reset to default 4

I would remend using addJavascriptInterface to create a proxy between Java and the webview Javascript

Yes I was able to set the javascript variable, which was declared global in the HTML-Javascript page and then use the

mWebView.loadUrl("javascript:(function () { " +
                           "setVariable("+valueToSetInHtmlPage+");" +
                           "})()");

and at the HTML webpage I had

function setVariables(a,b) {
    loginID = a; 
    serverUrl = b;
}

where loginID and serverUrl are both global

发布评论

评论列表(0)

  1. 暂无评论