<p>I am relatively new to JavaScript and am trying to understand how to use it correctly.</p>

<p>If I wrap JavaScript code in an anonymous function to avoid making variables <code>public</code> the functions within the JavaScript are not available from within the html that includes the JavaScript. </p>

<p>On initially loading the page the JavaScript loads and is executed but on subsequent reloads of the page the JavaScript code does not go through the execution process again. Specifically there is an ajax call using <code>httprequest</code> to get that from a PHP file and passes the returned data to a callback function that in <em>onsuccess</em> processes the data, if I could call the function that does the <code>httprequest</code> from within the html in a </p>

<pre><code>&lt;script type="text/javascript" &gt;&lt;/script&gt;
</code></pre>

<p>block on each page load I'd be all set - as it is I have to inject the entire JavaScript code into that block to get it to work on page load, hoping someone can educate me.</p>

[{'answer_id': 68026, 'author': 'HitScan', 'author_id': 9490, 'author_profile': 'https://Stackoverflow.com/users/9490', 'pm_score': 0, 'selected': False, 'text': "<p>It might be best not to wrap everything in an anonymous function and just hope that it is executed. You could name the function, and put its name in the body tag's onload handler. This should ensure that it's run each time the page is loaded.</p>\n"}, {'answer_id': 68046, 'author': 'HFLW', 'author_id': 252822, 'author_profile': 'https://Stackoverflow.com/users/252822', 'pm_score': 1, 'selected': False, 'text': '<p>I\'d suggest just doing window.onload:</p>\n\n<pre><code>&lt;script type="text/javascript"&gt;\n(function() {\n    var private = "private var";\n    window.onload = function() {\n        console.log(private);\n    }\n})();\n&lt;/script&gt;\n</code></pre>\n'}, {'answer_id': 68056, 'author': 'scunliffe', 'author_id': 6144, 'author_profile': 'https://Stackoverflow.com/users/6144', 'pm_score': 0, 'selected': False, 'text': '<p>Depends what you want to do, but to avoid polluting the global namespace, you could attach your code to the element you care about.</p>\n<p>e.g.</p>\n<pre class="lang-html prettyprint-override"><code>&lt;div id=&quot;special&quot;&gt;Hello World!&lt;/div&gt;\n&lt;script&gt;\n  (function(){\n    var foo = document.getElementById(\'special\');\n    foo.mySpecialMethod = function(otherID, newData){\n      var bar = document.getElementById(otherID);\n      bar.innerHTML = newData;\n    };\n    //do some ajax... set callback to call &quot;special&quot; method above...\n    doAJAX(url, \'get\', foo.mySpecialMethod);\n  })();\n&lt;/script&gt;\n</code></pre>\n<p>I\'m not sure if this would solve your issue or not, but its one way to handle it.</p>\n'}, {'answer_id': 68083, 'author': 'Anutron', 'author_id': 10071, 'author_profile': 'https://Stackoverflow.com/users/10071', 'pm_score': 2, 'selected': False, 'text': "<p>If you aren't using a javascript framework, I strongly suggest it. I use MooTools, but there are many others that are very solid (Prototype, YUI, jQuery, etc). These include methods for attaching functionality to the DomReady event. The problem with:</p>\n\n<pre><code>window.onload = function(){...};\n</code></pre>\n\n<p>is that you can only ever have one function attached to that event (subsequent assignments will overwrite this one).</p>\n\n<p>Frameworks provide more appropriate methods for doing this. For example, in MooTools:</p>\n\n<pre><code>window.addEvent('domready', function(){...});\n</code></pre>\n\n<p>Finally, there are other ways to avoid polluting the global namespace. Just namespacing your own code (mySite.foo = function...) will help you avoid any potential conflicts.</p>\n\n<p>One more thing. I'm not 100% sure from your comment that the problem you have is specific to the page load event. Are you saying that the code needs to be executed when the ajax returns as well? Please edit your question if this is the case.</p>\n"}, {'answer_id': 69473, 'author': 'user10109', 'author_id': 10109, 'author_profile': 'https://Stackoverflow.com/users/10109', 'pm_score': 1, 'selected': False, 'text': '<blockquote>\n  <p>On initially loading the page the js loads and is executed but on subsequent reloads of the page the js code does not go through the execution process again</p>\n</blockquote>\n\n<p>I\'m not sure I understand your problem exactly, since the JS should execute every time, no matter if it\'s an include, or inline script.  But I\'m wondering if your problem somehow relates to browser caching.  There may be two separate points of caching issues:  </p>\n\n<ol>\n<li><p>Your javascript include is being cached, and you are attempting to serve dynamically generated or recently edited javascript from this include.  </p></li>\n<li><p>Your ajax request is being cached.</p></li>\n</ol>\n\n<p>You should be able to avoid caching by <a href="http://www.google.com/search?q=force+no+cache" rel="nofollow noreferrer">setting response headers</a> on the server.<br>\nAlso, <a href="http://radio.javaranch.com/pascarello/2005/10/21/1129908221072.html" rel="nofollow noreferrer">this page</a> describes another way to get around caching issues from ajax requests.</p>\n'}]
['https://Stackoverflow.com/questions/68012', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']