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*/ });
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*/ });
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>