android webview returns blank page when load dynamic html page

Posted by user2962555 on Stack Overflow See other posts from Stack Overflow or by user2962555
Published on 2014-08-17T00:26:03Z Indexed on 2014/08/19 22:20 UTC
Read the original article Hit count: 164

Filed under:
|
|
|
|

I am trying to click one button to load a page into a div block dynamically. To test it, I try to append a list item with text "abc" into the loaded page. However, I always get a blank page. load function works fine because if I try to load a static page, it works. Following is my main html page code.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>LoadPageTest</title>
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:300,400,700">
    <link rel="stylesheet" href="./css/customizedstyle.css">
    <link rel="stylesheet" href="./css/themes/default/jquery.mobile-1.4.3.min.css">
    <link rel="stylesheet" href="./css/jqm-demos.css">
    <script src="./js/jquery.js"></script>
    <script scr="./js/customizedjs.js"></script>
    <script src="./js/jquery.mobile-1.4.3.min.js"></script>
    <script>
        $( document ).on( "pagecreate", "#demo-page", function() {

            $( document ).on( "swipeleft swiperight", "#demo-page", function( e ) {
                if ( $( ".ui-page-active" ).jqmData( "panel" ) !== "open" ) {
                    if ( e.type === "swipeleft" ) {
                        $( "#right-panel" ).panel( "open" );
                    } 
                }
            });
        });
        </script>

    <style type="text/css">
    body {
        overflow:hidden;
    }
    </style>
</head>
<body style= "overflow:hidden" scrolling="no">

<style type="text/css">
body {
    overflow:hidden;
}
</style>


<div data-role="page" id="main-page" style= "overflow:hidden" scrolling="no">

    <div role="main" class="ui-content" id ="maindiv" style= "overflow: auto">

        Will load diff pages here.

    </div><!-- /content -->

    <div data-role="panel" id="left-panel" data-theme="b">

        <ul data-role="listview" data-icon="false" id="menu">

            <li>
            <a href="#" id = "btnA" data-rel="close">Go Page A <img src="./images/icona.png" class="ui-li-thumb"/>
            </li>

            <li>
            <a href="#" id = "btnB" data-rel="close">Go Page B <img src="./images/iconb.png" class="ui-li-thumb"/>
            </li>

        </ul>

    </div><!-- /panel -->


    <script type="text/javascript">
        $("#btnA").on("click", function(){  

            $("#maindiv").empty();
            $("#maindiv").load("pageA.html");

        });

        $("#btnB").on("click", function(){  

            $("#maindiv").empty();

            $("#maindiv").load("pageB.html");
        });


    </script>

</div><!-- /page -->

</body>
</html>

Next is code for the page I try to load dynamically.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Page should be loaded</title>
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:300,400,700">
    <link rel="stylesheet" href="./css/customizedstyle.css">
    <link rel="stylesheet" href="./css/themes/default/jquery.mobile-1.4.3.min.css">
    <link rel="stylesheet" href="./css/jqm-demos.css">
    <script src="./js/jquery.js"></script>
    <script scr="./js/customizedjs.js"></script>
    <script src="./js/jquery.mobile-1.4.3.min.js"></script>
    <script>

        $(document).on('pagebeforeshow', function () {
            $('#postlist').append('<li> abc  </li>');
            $('#postlist').listview('refresh');
        });
    </script>

</head>
<body >

    <div data-role="page" id="posthome">
        <div data-role = "content">
            <ul data-role='listview' id = "postlist">

            </ul>
        </div>
    </div>
</body>
</html>

I doubt if it is because my javascript in the page doesn't work, cause the swipe js code in the main page seems not work either. Is that possible? I have enabled javascript in the onCreate() function of the activity file as below.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);

        new LongRunningGetIO().execute();

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.setWebViewClient(new AppClient());
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.loadUrl("file:///android_asset/index.html");
    }

I noticed there is a warning for statement to enable javascript "Using setJavaScriptEnabled can introduce XSS vulnerabilities into you application, review carefully". Will that maybe the reason? Then, I added @SuppressLint("SetJavaScriptEnabled") on top of the activity. The warning is gone, but the js code in pages seem still not work.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about android