如何在 webView 中启用 javascript
Posted
技术标签:
【中文标题】如何在 webView 中启用 javascript【英文标题】:How to enable javascript into webView 【发布时间】:2012-02-09 09:23:21 【问题描述】:在 android 中,如果我在 webView 中使用 javascript,它会强制关闭。有没有可能在 webView 中使用 java 脚本。请帮忙...
01-10 10:08:51.513: W/dalvikvm(5994): JNI WARNING: jarray 0x407447a8 points to non-array object (Ljava/lang/String;)
01-10 10:08:51.513: I/dalvikvm(5994): "WebViewCoreThread" prio=5 tid=9 NATIVE
01-10 10:08:51.513: I/dalvikvm(5994): | group="main" sCount=0 dsCount=0 obj=0x40567400 self=0x1f9e58
01-10 10:08:51.513: I/dalvikvm(5994): | sysTid=6010 nice=0 sched=0/0 cgrp=default handle=2269896
01-10 10:08:51.513: I/dalvikvm(5994): | schedstat=( 44479977019 41285891893 14352 )
01-10 10:08:51.513: I/dalvikvm(5994): at android.webkit.WebViewCore.nativeTouchUp(Native Method)
01-10 10:08:51.513: I/dalvikvm(5994): at android.webkit.WebViewCore.nativeTouchUp(Native Method)
01-10 10:08:51.543: I/dalvikvm(5994): at android.webkit.WebViewCore.access$3300(WebViewCore.java:53)
01-10 10:08:51.543: I/dalvikvm(5994): at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1158)
01-10 10:08:51.543: I/dalvikvm(5994): at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 10:08:51.543: I/dalvikvm(5994): at android.os.Looper.loop(Looper.java:123)
01-10 10:08:51.543: I/dalvikvm(5994): at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:629)
01-10 10:08:51.557: I/dalvikvm(5994): at java.lang.Thread.run(Thread.java:1019)
01-10 10:08:51.557: E/dalvikvm(5994): VM aborting
【问题讨论】:
【参考方案1】:默认情况下,WebView 中禁用 JavaScript。
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
【讨论】:
【参考方案2】: WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
【讨论】:
【参考方案3】:在Kotlin的WebView中启用JS:
val webView = web_view_id
webView.settings.javaScriptEnabled = true
【讨论】:
【参考方案4】:在某些版本中,您可能必须这样做:
WebView browser = new WebView();
WebEngine webEngine = browser.getEngine();
webEngine.setJavaScriptEnabled(true);
【讨论】:
【参考方案5】:要启用 JavaScript 创建,您需要为 WebView
init 创建一个 configuration
参数
struct VideoView: View, UIViewRepresentable
func makeUIView(context: Context) -> WKWebView
let configuration: WKWebViewConfiguration = WKWebViewConfiguration()
let preferences: WKWebpagePreferences = WKWebpagePreferences()
preferences.allowsContentJavaScript = true
configuration.defaultWebpagePreferences = preferences
let webView: WKWebView = WKWebView(
frame: .zero,
configuration: configuration
)
return webView
func updateUIView(_ UIView: WKWebView, context: Context)
guard let url = URL("") else return
UIView.scrollView.isScrollEnabled = false
UIView.load(URLRequest(url: url))
【讨论】:
以上是关于如何在 webView 中启用 javascript的主要内容,如果未能解决你的问题,请参考以下文章
如何在 WebView 中启用 GoogleTV 上的 Flash