on master help sign in
Merge pull request #874 from puffnfresh/javascript-ready

Execute JavaScript output after DOMContentLoaded

commitba03e213ef15be4654422a581b224ce701ce7b2e authorEdwin Brady <edwin.brady@gmail.com> dateThu 13 Feb 2014, 20:00 (13 years ago) parent57af9858561f75
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