how to call parameterized javascript function in t

2019-05-10 00:57发布

问题:

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.

回答1:

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



回答2:

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.