admin管理员组

文章数量:1254262

I am trying to pass 2 parameters to a javascript function.This code webview.loadUrl("javascript: function_to_call();"); works fine without parameters but i couldn't use it with parameters.

This is javascript junction :

function changeLocation(_lon , _lat){
    var zoom=16;
    var lonLat = new OpenLayers.LonLat( _lon , _lat ).transform(         
        new OpenLayers.Projection("EPSG:4326"), 
        map.getProjectionObject());

    map.setCenter (lonLat, zoom);
}

And this is how i call it from java :

webView.loadUrl("javascript:changeLocation( -0.1279688 ,51.5077286 );") ;

Edit: I couldn't find the problem and i changed my approach, now i am injecting whole javascript function with desired changes everytime when i need to. It is not best solution but it works. Thank you everyone for your help.

I am trying to pass 2 parameters to a javascript function.This code webview.loadUrl("javascript: function_to_call();"); works fine without parameters but i couldn't use it with parameters.

This is javascript junction :

function changeLocation(_lon , _lat){
    var zoom=16;
    var lonLat = new OpenLayers.LonLat( _lon , _lat ).transform(         
        new OpenLayers.Projection("EPSG:4326"), 
        map.getProjectionObject());

    map.setCenter (lonLat, zoom);
}

And this is how i call it from java :

webView.loadUrl("javascript:changeLocation( -0.1279688 ,51.5077286 );") ;

Edit: I couldn't find the problem and i changed my approach, now i am injecting whole javascript function with desired changes everytime when i need to. It is not best solution but it works. Thank you everyone for your help.

Share Improve this question edited Jun 19, 2016 at 19:49 sambomartin 6,8137 gold badges43 silver badges64 bronze badges asked Nov 16, 2010 at 15:03 dirhemdirhem 6211 gold badge12 silver badges25 bronze badges 6
  • @dirhem: Is this loadUrl() (note: capital U) on the WebView widget in Java? Or something else? – CommonsWare Commented Nov 16, 2010 at 15:11
  • @ CommonsWare Sorry i just mistyped here and yes it is WebView widget's loadUrl function. – dirhem Commented Nov 16, 2010 at 15:18
  • Could you post the source code that is giving you trouble? Also, are there any error messages? – Jake Commented Nov 16, 2010 at 15:22
  • @ Jake I have posted the source coude and no i am not having any error message. – dirhem Commented Nov 16, 2010 at 15:46
  • 1 did you try webView.loadUrl('javascript:changeLocation( "-0.1279688" ,"51.5077286" );') ;? – josh.trow Commented Nov 16, 2010 at 16:06
 |  Show 1 more ment

2 Answers 2

Reset to default 8

What you have looks fine. Here is a sample project that demonstrates an almost identical syntax.

Try changing webView.loadUrl("javascript:changeLocation( -0.1279688 ,51.5077286 );") ;

to webView.loadUrl("javascript:changeLocation( '-0.1279688' ,'51.5077286' );") ; and maybe getting rid of the ;

I just had a similar problem and I fixed it by adding the '' around my parameter. I didn't have a semicolon in my solution either and it worked so you may need to remove it.

本文标签: androidhow to call parameterized javascript function in the WebKitStack Overflow