| Jun | JUL | Aug |
| 15 | ||
| 2019 | 2020 | 2021 |
COLLECTED BY
Collection: Save Page Now
local js = require "js"
local window = js.global
window:alert("Hello from Fengari!")
Try it:
▶
local js = require "js"
local window = js.global
window:alert("Hello from Fengari!")
local js = require "js"
local window = js.global
local document = window.document
print("Document's title: " .. document.title)
Try it:
▶
local js = require "js"
local window = js.global
local document = window.document
print("Document's title: " .. document.title)
Lua in the browser means you can use coroutines to write beautiful asynchronous code:
local js = require "js"
local window = js.global
local function sleep(delay)
local co = assert(coroutine.running(), "Should be run in a coroutine")
window:setTimeout(function()
assert(coroutine.resume(co))
end, delay*1000)
coroutine.yield()
end
coroutine.wrap(function()
print "Going to sleep now..."
sleep(3)
print "Sleep well?"
end)()
Try it:
▶
local js = require "js"
local window = js.global
local function sleep(delay)
local co = assert(coroutine.running(), "Should be run in a coroutine")
window:setTimeout(function()
assert(coroutine.resume(co))
end, delay*1000)
coroutine.yield()
end
local print = _G.print
coroutine.wrap(function()
print "Going to sleep now..."
sleep(3)
print "Sleep well?"
end)()
$ curl -L -O https://github.com/fengari-lua/fengari-web/releases/download/v0.1.4/fengari-web.js
Include it in your webpage:
<script src="fengari-web.js" type="text/javascript"></script>
Now any script of type application/lua will be run by fengari:
<script type="application/lua">
print("hello world!")
</script>
<script src="/my-script.lua" type="application/lua" async></script>