如何在WebKit中调用参数化的javascript函数?
我正在尝试将 2 个参数传递给 javascript 函数。此代码 webview.loadUrl("javascript: function_to_call();");
在没有参数的情况下工作正常,但我无法将其与参数一起使用。
这是 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);
}
这就是我从 java 调用它的方式:
webView.loadUrl("javascript:changeLocation( -0.1279688 ,51.5077286 );") ;
编辑:我找不到问题,我改变了我的方法,现在我每次需要时都会注入整个 javascript 函数并进行所需的更改。这不是最好的解决方案,但它有效。谢谢大家的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将
webView.loadUrl("javascript:changeLocation( -0.1279688 ,51.5077286 );") ;
更改为
webView.loadUrl("javascript:changeLocation( '-0.1279688' ,'51.5077286' ); ”);
也许摆脱
;
我刚刚遇到了类似的问题,我通过在参数周围添加
''
来修复它。我的解决方案中也没有分号,但它有效,因此您可能需要将其删除。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.你所拥有的看起来不错。 这是一个示例项目,它演示了几乎相同的语法。
What you have looks fine. Here is a sample project that demonstrates an almost identical syntax.