on master help sign in
Execute JavaScript output after DOMContentLoaded

Previously, JavaScript generated for the browser would only execute on the DOMContentLoaded event. We should also check if the browser is already ready. We can do this with the following: document.readyState == "complete" || document.readyState == "loaded"

commit8561f757ecd29697105570287d7fe3815a4610ee authorBrian McKenna <brian@brianmckenna.org> dateMon 10 Feb 2014, 09:20 (13 years ago) parent98ddd5b
1 file changed+14−2
src/IRTS/CodegenJavaScript.hs+14−2
src/IRTS/CodegenJavaScript.hs+14−2
@@ -231,6 +231,10 @@ jsAnd :: JS -> JS -> JS
231231jsAnd = JSBinOp "&&"
232232
233233
234+jsOr :: JS -> JS -> JS
235+jsOr = JSBinOp "||"
236+
237+
234238jsType :: JS
235239jsType = JSIdent $ idrRTNamespace ++ "Type"
236240
@@ -1247,17 +1251,25 @@ codegenJavaScript target definitions filename outputType = do
12471251 JSAlloc "main" $ Just $ JSFunction [] (
12481252 case target of
12491253 Node -> mainFun
1250− JavaScript -> jsMeth (JSIdent "window") "addEventListener" [
1254+ JavaScript -> JSCond [(isReady, mainFun), (JSTrue, jsMeth (JSIdent "window") "addEventListener" [
12511255 JSString "DOMContentLoaded", JSFunction [] (
12521256 mainFun
12531257 ), JSFalse
1254− ]
1258+ ])]
12551259 )
12561260 where
12571261 mainFun :: JS
12581262 mainFun = jsTailcall $ jsCall runMain []
12591263
12601264
1265+ isReady :: JS
1266+ isReady = readyState `jsEq` JSString "complete" `jsOr` readyState `jsEq` JSString "loaded"
1267+
1268+
1269+ readyState :: JS
1270+ readyState = JSProj (JSIdent "document") "readyState"
1271+
1272+
12611273 runMain :: String
12621274 runMain = idrNamespace ++ translateName (sMN 0 "runMain")
12631275