Syntax error when attempting to use the `new` JS operator
Forcoy opened this issue · comments
Forcoy commented
local js = require("js")
local window = js.global
local function loadmod(path)
local http = window.new window.XMLHttpRequest -- syntax error near 'http'
http:open("GET",path,false)
http:send(nil)
print(http.responseText)
end
loadmod("http://127.0.0.1:5500/dumbtest.txt")
How do I avoid this? (moving window.new to be before the variable is set doesn't work either).
Hai commented
local js = require "js"
local global = js.global
local function loadmod(url)
local xhr = js.new(global.XMLHttpRequest)
xhr.onload = function(e)
if e.status >= 200 and e.status <= 299 then
print(e.responseText)
return true
end
print("error:" .. url)
end
xhr:open("GET", url, true)
xhr:send()
end
loadmod("http://127.0.0.1:5500/dumbtest.txt")
Forcoy commented
Thank you!
daurnimator commented
xhr.onload = function(e)
Doesn't this have to be
xhr.onload = function(this, e)
or alternatively
function xhr:onload(e)