How can I call Android JavaScript functions in WebView?

I want to call a function written in .cs code file in a .js (javascript). How to do this?   (There a way to call this cs function through Ajax call but dont have much idea about the same.)

  • For example there is a class  Sample.cs public class Test {  publiv void TestFunction()  {   // set of code  } } the TestFunction is to be called in the javascript function <script type="C#/javascript"> function TestCall() {  Test.TestFunction();//////-- //(this is a Cs function, which is to be called, have a look to the Class Test, the defination of the TestFunction is written in that, so this call has to be made) } </script>and this javascript function will be called on some event on the html/ aspx page. For example -- onClick="Testcall();"

  • Answer:

    is probably the easiest way to do this, if the JavaScript is running in a browser.

Toby Thain at Quora Visit the source

Was this solution helpful to you?

Other answers

It really depends that which technology stack are you in. I have asp.net background so I will answer with that perspective. If you are working on http://asp.net web forms and you need to call the code behind methods in the Java Script then page methods is the way to achieve this. You can find basic tutorial here http://www.ajaxtutorials.com/quickstart/ajax-tutorial-page-methods-in-asp-net/ You can also use web api or web service to achieve the same. You cs file will be served through wep api request using java scripts calls. Following detailed step by step tutorial will help you to understand the wep api approach. http://www.codeproject.com/Tips/678138/MVC-using-Web-API-and-JQuery-to-GET-or-POST-Data

Muhammad Haseeb Asif

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.