2015-12-25 74 views
1

我已經創建了一個簡單的web視圖功能的android應用程序。在我的html頁面加載webview,我使用下面的腳本。JavaScript的警告消息不能在Android應用程序工作

var message="!!YOU CANNOT COPY ANY TEXT OR IMAGE FROM THIS APP!"; 
    function clickIE4() 
    { 
    if (event.button==2) 
    { 
     alert(message); 
     return false; 
    } 
    } 
    function clickNS4(e) 
    { 
    if (document.layers||document.getElementById&&!document.all) 
    { 
     if (e.which==2||e.which==3) 
     { 
     alert(message); 
     return false; 
     } 
    } 
    } 

    if (document.layers) 
    { 
    document.captureEvents(Event.MOUSEDOWN); 
    document.onmousedown=clickNS4; 
    } 
    else if (document.all&&!document.getElementById) 
    { 
    document.onmousedown=clickIE4; 
    } 
    document.oncontextmenu=new Function("alert(message);return false") 
//for disable select option 
//document.onselectstart = new Function("alert(message);return false"); 

當我在android webview應用程序中打開它時,它不顯示警告消息。當我在普通瀏覽器中打開它時,它工作正常。

+1

也爲web視圖添加java代碼 – Tasos

+0

好的..但哪個函數可以用於我??? –

回答

2

您需要創建WebChromeClient並設置爲WebiView。也覆蓋onJsAlert方法。

 WebChromeClient webChromeClient = new WebChromeClient() { 
       @Override 
       public boolean onJsAlert(WebView view, String url, String message, final JsResult result) { 

        new AlertDialog.Builder(context) 
          .setMessage(message) 
          .setPositiveButton(android.R.string.ok, 
            new DialogInterface.OnClickListener() { 
             public void onClick(DialogInterface dialog, int which) { 
              result.confirm(); 
             } 
            }) 
          .create() 
          .show(); 

        return true; 
       } 
    } 

    webView.setWebChromeClient(webChromeClient);