2

I have coded a game in HTML and JavaScript. I had loaded the HTML file locally into the WebView but it had not loaded the JavaScript. I was wondering how I could get the HTML to use the JavaScript. Any help is greatly appreciated.

1
  • Where do you put the HTML and Javascript currently? Within assets folder? Commented Oct 25, 2011 at 21:32

5 Answers 5

4

Try this code.. This code works for.. Hope it will be helpfull to u also.

WebView webView;
        webView = (WebView) findViewById(R.id.wbView);
        webView.getSettings().setPluginsEnabled (true);
        webView.getSettings().setJavaScriptEnabled (true);
Sign up to request clarification or add additional context in comments.

Comments

2

You can check here: http://developer.android.com/resources/tutorials/views/hello-webview.html

in order to get a general perspective of js with webview and more specifically:

mWebView.getSettings().setJavaScriptEnabled(true);

Hope this helps!

2 Comments

It doesn't work. the HTML loads 2 javascripts locally. Is there another way?
Please add some code in order to better understand your problem.
0

you can try this :

String html = "<html><body>Hello, World!</body></html>";
String mime = "text/html";
String encoding = "utf-8";
WebView myWebView = (WebView)this.findViewById(R.id.myWebView);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadDataWithBaseURL(null, html, mime, encoding, null);

Comments

0

Just load HTML file URI like this:

webView.loadUrl("file://" + getContext().getExternalCacheDir() + "/editor.html");

Hope it can help you!

Comments

0
    binding.webview.settings.javaScriptEnabled=true
    binding.webview.loadUrl("file:///android_asset/GeneralCF.html")

    binding.your_view.setOnClickListener {
        // load js on click button
        binding.webview.loadUrl("javascript:myFunction(\"$str_name\")")
    }

index.js

function myFunction(str_name)
{
document.getElementById("demo").innerHTML = str_name;
}

GeneralCF.html

        <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="Generator" content="EditPlus®">
        <meta name="Author" content="">
        <meta name="Keywords" content="">
        <meta name="Description" content="">
        <title>Document</title>
        <script src="index.js"></script>
    
    </head>
    <body>
    
    <h1>This is my first webpage</h1>
    
    <p id="demo">A Paragraph.</p>
    
    <button type="button" onclick="myFunction()">Try it</button>
    
    
    </body>
    </html>

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.