Js与WebView交互需注意的

一、Java代码

1、webView的设置

webView.getSettings().setDefaultTextEncodingName("utf-8");

webView.getSettings().setJavaScriptEnabled(true);

webView.loadUrl("http://shaoml.oicp.net/mt_app_int/weixin/around.jsp?mobile=" + phone + "&token=" + token);

// WebChromeClient主要辅助WebView处理Javascript的对话框、网站图标、网站title、加载进度等

// webChromeClient = new WebChromeClient() {

// @Override

// public void onReceivedTitle(WebView view, String title) {

// super.onReceivedTitle(view, title);

// // tv_title_content.setText(title);

// SunriseUIHelper.showTip(Around_Activity.this, "当前页面Title:" + title);

// wv_list.getSettings().setJavaScriptEnabled(true);

// }

// };

// wv_list.setWebChromeClient(webChromeClient);

webView_list.addJavascriptInterface(this, "todetail");

 

 

/**

 * 

 *按返回的html地址跳转页面

     *html路径要是绝对路径

 * @param year

 */

@JavascriptInterface

public void refresh(String html, String name) {// 跳转到商家详情页面

Intent adetailIntent = new Intent(Around_Activity.this, Around_Detail_Activity.class);

adetailIntent.putExtra("html", "http://shaoml.oicp.net/mt_app_int/weixin/" + html);

adetailIntent.putExtra("name", name);

startActivity(adetailIntent);

}

 

二、Js代码

(1)直接传

 window.todetail.refresh('http://mob.com',"全家");

 

(2)当变量传

getShopUrl = function(obj){

var sellerid = $(obj).parent().attr("sellerid");

var url = "\aroundInfo.jsp?sellerid="+sellerid;

var sellerTitle = $(obj).children(".aroundLi").children(".aroundLiTit").html();

    if(window.todetail != undefined){

    //要记得加上“”,这样java那边才知道是字符串类型,才能做对应的操作。

    window.todetail.refresh(url+"",sellerTitle+"");

    }else{

      location.href=url;  

    }

   };

转载于:https://www.cnblogs.com/PrincessMiao/p/4771132.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值