{"id":813,"date":"2022-08-30T15:07:36","date_gmt":"2022-08-30T15:07:36","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/how-to-get-some-revealing-module-code-to-only-run-after-dom-ready-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:07:36","modified_gmt":"2022-08-30T15:07:36","slug":"how-to-get-some-revealing-module-code-to-only-run-after-dom-ready-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-get-some-revealing-module-code-to-only-run-after-dom-ready-collection-of-common-programming-errors\/","title":{"rendered":"How to get some revealing module code to only run after DOM ready?-Collection of common programming errors"},"content":{"rendered":"<p>I believe what you&#8217;re looking for is something like this:<\/p>\n<p>notice the difference between these two examples:<\/p>\n<pre><code>\/\/working\nvar myNamespace = (function($) {\n    $(function(){\n        var init = (function(){\n            var text = document.createTextNode(\"Everything's ready!\");\n            document.body.appendChild(text);\n        })();\n    });\n\n}(jQuery));\n\n\/\/Error: Cannot call method 'appendChild' of null \nvar myNamespace = (function($) {\n\n        var init = (function(){\n            var text = document.createTextNode(\"Everything's ready!\");\n            document.body.appendChild(text);\n        })();\n\n\n}(jQuery));\n<\/code><\/pre>\n<p>Though, what&#8217;s happening here is that <strong>init()<\/strong> is effectively not accessible by anything else within the namespace, because it&#8217;s taking place after the DOM has loaded.<\/p>\n<p>Nonetheless, this is close to what you&#8217;re having a problem with, I think&#8230;<\/p>\n<p>I&#8217;m assuming that you want the DOM ready wrapper within your module for organizational purposes.<\/p>\n<p>Also note that you have access to the module as you would normally from outside of the module:<\/p>\n<pre><code>\/\/appends to the body: \"Everything's ready!undefined\nvar myNamespace = (function($) {\n   var private = \" Hello world\";\n        $(function(){\n            var hello =(function(){\n                var text = document.createTextNode(\"Everything's ready!\" +myNamespace.private);\n                document.body.appendChild(text);\n            }());\n        });\n\n    return{\n        public: \" Hello World\"   \n    }\n\n}(jQuery));\n\n\n\n \/\/works as expected \n    var myNamespace = (function($) {    \n     var private = \" Hello world\";\n     $(function(){\n      var hello =(function(){\n       var text = document.createTextNode(\"Everything's ready!\" +myNamespace.public);\n       document.body.appendChild(text);\n      }());\n     });\n\n     return{\n      public: \" Hello World\"   \n     }   }(jQuery));\n<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:47:07. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I believe what you&#8217;re looking for is something like this: notice the difference between these two examples: \/\/working var myNamespace = (function($) { $(function(){ var init = (function(){ var text = document.createTextNode(&#8220;Everything&#8217;s ready!&#8221;); document.body.appendChild(text); })(); }); }(jQuery)); \/\/Error: Cannot call method &#8216;appendChild&#8217; of null var myNamespace = (function($) { var init = (function(){ var text [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-813","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/813","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=813"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/813\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}