Browse Source

Run React app only when DOM content is loaded

Konstantin Tarkus 9 years ago
parent
commit
b526067910
1 changed files with 11 additions and 1 deletions
  1. 11 1
      app.js

+ 11 - 1
app.js

@@ -19,7 +19,7 @@ const route = async (path, callback) => {
   await callback(<Layout>{React.createElement(component)}</Layout>);
   await callback(<Layout>{React.createElement(component)}</Layout>);
 };
 };
 
 
-if (canUseDOM) {
+function run() {
   const container = document.getElementById('app');
   const container = document.getElementById('app');
   Location.listen(location => {
   Location.listen(location => {
     route(location.pathname, async (component) => ReactDOM.render(component, container, () => {
     route(location.pathname, async (component) => ReactDOM.render(component, container, () => {
@@ -29,4 +29,14 @@ if (canUseDOM) {
   });
   });
 }
 }
 
 
+if (canUseDOM) {
+// Run the application when both DOM is ready
+// and page content is loaded
+  if (window.addEventListener) {
+    window.addEventListener('DOMContentLoaded', run);
+  } else {
+    window.attachEvent('onload', run);
+  }
+}
+
 export default { route, routes };
 export default { route, routes };