如何在 WebKit 中调用参数化 javascript 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4195514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to call parameterized javascript function in the WebKit?
提问by dirhem
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.
我正在尝试将 2 个参数传递给 javascript 函数。此代码在webview.loadUrl("javascript: function_to_call();");没有参数的情况下工作正常,但我无法将其与参数一起使用。
This is javascript junction :
这是 javascript 连接:
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 :
这就是我从 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.
编辑:我找不到问题,我改变了我的方法,现在我每次需要时都注入具有所需更改的整个 javascript 函数。这不是最好的解决方案,但它有效。感谢大家的帮助。
回答by CommonsWare
What you have looks fine. Here is a sample projectthat demonstrates an almost identical syntax.
你所拥有的看起来不错。这是一个示例项目,演示了几乎相同的语法。
回答by Miles Morales
Try changing webView.loadUrl("javascript:changeLocation( -0.1279688 ,51.5077286 );") ;
尝试改变 webView.loadUrl("javascript:changeLocation( -0.1279688 ,51.5077286 );") ;
to webView.loadUrl("javascript:changeLocation( '-0.1279688' ,'51.5077286' );") ;and maybe getting rid of the ;
对webView.loadUrl("javascript:changeLocation( '-0.1279688' ,'51.5077286' );") ;,也许摆脱的;
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.
我刚刚遇到了类似的问题,我通过''在我的参数周围添加来解决它。我的解决方案中也没有分号并且它有效,因此您可能需要将其删除。

