2

I'm trying to load an external JavaScript file into an HTML file. Both are placed in the assets folder.

This is my htmlTest.html file:

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    <script src="test.js"></script>
</head>

<body>

    <script type="text/javascript">

        func();


    </script>

</body>

And this is my test.js file:

function func(){
    $(document).ready(function () {
        $('#EDIFICI').children().click(function () {
            alert($(this).attr('id'));
        });

    });
}   

Both files are located in the root of the assets folder. I load htmlTest.html into my WebView like this:

wv.loadUrl("file:///android_asset/htmlTest.html");  

If I put JavaScript code inline directly inside the HTML page it works, but it doesn't when I link an external JavaScript file.

5 Answers 5

1

1.on onCreate add getSettings().setJavaScriptEnabled(true)

2.change .js like this

<head>
    <script type="text/javascript" src="test.js"></script>
</head>

Sign up to request clarification or add additional context in comments.

Comments

1

This worked for me:

 <head>
        <script type="text/javascript" src="file:///android_asset/xxxxx.js"></script>
</head>

And xxxxx.js is in the assets folder.

In the code

WebView.enableSlowWholeDocumentDraw();
        oWebView.clearCache(true);
        oWebView.setPadding(0, 0, 0, 0);
        oWebView.setInitialScale(1);
        oWebView.getSettings().setUseWideViewPort(true);
        oWebView.getSettings().setLoadWithOverviewMode(true);
        oWebView.getSettings().setSupportZoom(true);
        oWebView.getSettings().setBuiltInZoomControls(true);
        oWebView.setScrollbarFadingEnabled(false);
        oWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        oWebView.getSettings().setJavaScriptEnabled(true);
        oWebView.setWebViewClient(new WebViewClient());

Comments

0

Try something like this

webView.evaluateJavascript(yourFileAsString)

2 Comments

It didn't help.
Variable yourFileAsString contains valid javascript or you pass file path?
0

Try to add your script as follow

<html>
  <head>
    <script type="text/javascript" src="test.js"></script>
  </head>

And add test.js file to folder with html

Comments

0

There was an error in my javascript file. It works like I stated in my example. Sorry for wasting your time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.