Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
186
rated 0 times [  190] [ 4]  / answers: 1 / hits: 21257  / 11 Years ago, sat, july 13, 2013, 12:00:00

I am calling javascript function from an activity but I am getting Uncaught ReferenceError: myFunction is not defined at null:1 error. Here is my files



MainActivity.java



package com.example.androidexample;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

WebView webview = (WebView)this.findViewById(R.id.webView1);
WebSettings webSettings = webview.getSettings();

// Enable Javascript for interaction
webSettings.setJavaScriptEnabled(true);

// Make the zoom controls visible
webSettings.setBuiltInZoomControls(true);

// Allow for touching selecting/deselecting data series
webview.requestFocusFromTouch();

// Set the client
webview.setWebViewClient(new WebViewClient());
webview.setWebChromeClient(new WebChromeClient());


//webview.loadUrl(file:///android_asset/test.html);

/*String str = <books> +
<book title=A Tale of Two Cities/> +
<book title=1984/> +
</books>;*/




webview.loadUrl(file:///android_asset/test.html);
webview.loadUrl(javascript:myFunction());
//webview.loadUrl(javascript:myFunction(+str+));

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}


test.html



<html>
<body>
<script type=text/javascript src=marknote.js></script>
<script type=text/javascript>
function myFunction()
{
var str = '<books><book></book></books>';
var parser = new marknote.Parser();
var doc = parser.parse(str);

var bookEls = doc.getRootElement().getChildElements();

for (var i=0; i<bookEls.length; i++) {
var bookEl = bookEls[i];

alert(Element name is + bookEl.getName());
}
}
</script>

</body>
</html>


I have seen this error occurred when I am using webview.loadUrl(javascript:myFunction());. I want to pass a xml string from java and parse it into javascript. Please help me to find a solution.


More From » android

 Answers
25

You should execute the javascript when the page is loaded



webview.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url){
webview.loadUrl(javascript:myFunction());
}
});


The code will be executed and will find your javascript function. The way you are doing now does not wait.


[#77019] Thursday, July 11, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cruz

Total Points: 415
Total Questions: 98
Total Answers: 109

Location: France
Member since Thu, May 6, 2021
3 Years ago
cruz questions
Tue, Sep 14, 21, 00:00, 3 Years ago
Fri, Mar 26, 21, 00:00, 3 Years ago
Sat, Oct 26, 19, 00:00, 5 Years ago
;