如何在WebKit中调用参数化的javascript函数?

发布于 2024-10-02 17:23:39 字数 736 浏览 1 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸里的那抹悲凉 2024-10-09 17:23:40

尝试将 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.

随心而道 2024-10-09 17:23:39

你所拥有的看起来不错。 这是一个示例项目,它演示了几乎相同的语法。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文