1

JavaScript Function

    <!DOCTYPE html>
   <html>
   <head>
  <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width; user-scalable=0;" />
    <title>My HTML</title>
   </head>
     <body>
        <h1>MyHTML</h1>
       <p id="mytext">Hello!</p>

        function callFromActivity(msg){
           alert("Hello! I am an alert box!");
          document.getElementById("mytext").innerHTML = msg;
       }


   </script>

  </body>
  </html>

Android Code

myWebView.loadUrl("file:///android_asset/mypage.html");
String msgToSend = "message";
myWebView.loadUrl("javascript:callFromActivity(\"" + msgToSend
            + "\")");

on Button Clicked event java script function call correctly. but i am not able to call java script function without button click.

2
  • 1
    Did you setted setJavascriptEnabled(true)? Commented Jun 4, 2012 at 9:55
  • yes, myBrowser.getSettings().setJavaScriptEnabled(true); Commented Jun 4, 2012 at 10:14

2 Answers 2

1

Your javascript is wrong formatted. You should put it in such tags

<script type="text/javascript">
    //your JS
</script>

So, your code will looks like that

 <script type="text/javascript">
 function callFromActivity(msg){
    alert("Hello! I am an alert box!");
   document.getElementById("mytext").innerHTML = msg;
 }
 </script>
Sign up to request clarification or add additional context in comments.

Comments

0

try this ,

     String msgToSend = "message";
     String js = "msg = "+msgToSend+";"
                 +"function callFromActivity(msg){"
                 +"alert('Hello! I am an alert box!');"
                 +"document.getElementById('mytext').innerHTML = msg;";

      view.loadUrl("javascript:(function(){" + js + "})()");

1 Comment

i want to do in HTML side not android side

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.