【问题标题】:Reference to local JS and CSS files inside local HTML file引用本地 HTML 文件中的本地 JS 和 CSS 文件
【发布时间】:2015-04-16 11:15:03
【问题描述】:

让我们看看这个例子:Loading existing .html file with android WebView 我加载了我的 HTML 文件,但它包含对其他 local css 和 js 文件的引用。我是否需要在 HTML 文件中使用相同的 file:/// URL?

【问题讨论】:

    标签: android webview


    【解决方案1】:

    不,你的结构应该是这样的:

    /assets/html/index.html

    /assets/scripts/index.js

    /assets/css/index.css

    那就做吧 (Android WebView: handling orientation changes)

        //in onCreate() for Activity, or in onCreateView() for Fragment
        if(WebViewStateHolder.INSTANCE.getBundle() == null) { //this works only on single instance of webview, use a map with TAG if you need more
            webView.loadUrl("file:///android_asset/html/index.html");
        } else {
            webView.restoreState(WebViewStateHolder.INSTANCE.getBundle());
        }
    

    确保添加

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            webSettings.setAllowFileAccessFromFileURLs(true);
            webSettings.setAllowUniversalAccessFromFileURLs(true);
        }
    

    那么就使用url

    <html>
    <head>
        <meta charset="utf-8">
        <title>Zzzz</title>
        <script src="../scripts/index.js"></script>
        <link rel="stylesheet" type="text/css" href="../css/index.css">
    

    编辑:

    只有在您添加我链接的代码时,该特定代码才能工作

    public class WebViewFragment extends Fragment { //could be activity
        private enum WebViewStateHolder
        {
            INSTANCE;
    
            private Bundle bundle;
    
            public void saveWebViewState(WebView webView)
            {
                bundle = new Bundle();
                webView.saveState(bundle);
            }
    
            public Bundle getBundle()
            {
                return bundle;
            }
        }
    
        @Override
        public void onPause()
        {
            WebViewStateHolder.INSTANCE.saveWebViewState(myWebView);
            super.onPause();
        }
    

    至于你所说的任何“安全问题”,你没有分享足够的数据。

    【讨论】:

    • if(WebViewStateHolder.INSTANCE.getBundle() == null) { //这仅适用于 webview 的单个实例,如果您需要更多 webView.loadUrl("file:/ //android_asset/html/index.html"); } else { webView.restoreState(WebViewStateHolder.INSTANCE.getBundle());不起作用,并且脚本中的引用似乎存在安全问题。 (无法加载文件)
    • there seems to be a security issue with the reference inside the script. (cannot load file)我不明白你的意思。
    • 它说文件不存在(参考文献中的那个)
    • 那么你应该把它放在那里,因为显然它不在那里!我从现有的工作代码中获得了此代码,因此您的结构有误。它在src\main\assets 文件夹中吗?
    【解决方案2】:

    根据Rendering HTML in a WebView with custom CSS,您不必指定每个CSS或JS。将它们包含在 HTML 文件本身中。

    【讨论】:

      猜你喜欢
      • 2018-04-07
      • 1970-01-01
      • 2011-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-21
      • 2017-02-17
      • 1970-01-01
      相关资源
      最近更新 更多