2016-09-14 253 views
0

我使用“lua.vm.js”在网页客户端使用lua进行开发。如何从Javascript调用Lua函数

我想知道如何从js脚本调用Lua函数。

var element = document.getElementById("myBtn") 
    element.addEventListener("click", function(){ /*call here Lua function*/ }); 
+1

我怀疑,你发现在互联网上什么都没有。 lua.vm.js网页提供了一个示例... – Piglet

+2

这是一个[解决方法](http://pastebin.com/ck1rmRmK) –

回答

1

方法#1
操纵的JavaScript从Lua代码中的对象:

<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> 

方法#2
使用L.execute(code)从JS执行Lua代码:

简单的例子:
element.addEventListener("click", function(){ L.execute('YourLuaFunction()'); });

朗例如:

<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>