@@ -231,6 +231,10 @@ jsAnd :: JS -> JS -> JS
231jsAnd = JSBinOp "&&"
231jsAnd = JSBinOp "&&"
234+jsOr :: JS -> JS -> JS
234jsType :: JS
238jsType :: JS
235jsType = JSIdent $ idrRTNamespace ++ "Type"
239jsType = JSIdent $ idrRTNamespace ++ "Type"
@@ -1247,17 +1251,25 @@ codegenJavaScript target definitions filename outputType = do
1247 JSAlloc "main" $ Just $ JSFunction [] (
1251 JSAlloc "main" $ Just $ JSFunction [] (
1248 case target of
1252 case target of
1249 Node -> mainFun
1253 Node -> mainFun
1250− JavaScript -> jsMeth (JSIdent "window") "addEventListener" [
1254+ JavaScript -> JSCond [(isReady, mainFun), (JSTrue, jsMeth (JSIdent "window") "addEventListener" [
1251 JSString "DOMContentLoaded", JSFunction [] (
1255 JSString "DOMContentLoaded", JSFunction [] (
1253 ), JSFalse
1257 ), JSFalse
1257 mainFun :: JS
1261 mainFun :: JS
1258 mainFun = jsTailcall $ jsCall runMain []
1262 mainFun = jsTailcall $ jsCall runMain []
1266+ isReady = readyState `jsEq` JSString "complete" `jsOr` readyState `jsEq` JSString "loaded"
1270+ readyState = JSProj (JSIdent "document") "readyState"
1261 runMain :: String
1273 runMain :: String
1262 runMain = idrNamespace ++ translateName (sMN 0 "runMain")
1274 runMain = idrNamespace ++ translateName (sMN 0 "runMain")