0

I use "lua.vm.js" to develop with lua in web client side.

I want to know how can i call a Lua function from js script.

var element = document.getElementById("myBtn")
  element.addEventListener("click", function(){ /*call here Lua function*/ });
2
  • 1
    I doubt that you found nothing on the internet. the lua.vm.js webpage evengives an example... Commented Sep 14, 2016 at 19:41
  • 2
    The is a workaround Commented Sep 14, 2016 at 19:53

1 Answer 1

3

Method #1
Manipulate JavaScript objects from inside Lua code:

<script src="lua.vm.js"></script>
<script type="text/lua">
    function YourLuaFunction()
       -- your Lua code is here
    end
</script>

<button id="MyBtn">Lua inside</button>
<script type="text/lua">
    js.global.document:getElementById("MyBtn"):addEventListener("click", YourLuaFunction);
</script>

Method #2
Use L.execute(code) to execute Lua code from JS:

Short example:
element.addEventListener("click", function(){ L.execute('YourLuaFunction()'); });

Long example:

<script src="lua.vm.js"></script>
<script> 
   function executeLua(code) { 
      try { L.execute(code); } catch(e) { alert(e.toString()); } 
   } 
</script>
<script type="text/lua">
      function YourLuaFunction()
         -- your Lua code is here
      end
</script>

<button onclick="executeLua('YourLuaFunction()')">Exec Lua code</button>
Sign up to request clarification or add additional context in comments.

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.