<p>I have a javascript function that manipulates the DOM when it is called (adds CSS classes, etc).  This is invoked when the user changes some values in a form.  When the document is first loading, I want to invoke this function to prepare the initial state (which is simpler in this case than setting up the DOM from the server side to the correct initial state).</p>

<p>Is it better to use window.onload to do this functionality or have a script block after the DOM elements I need to modify?  For either case, why is it better?</p>

<p>For example:</p>

<pre><code>function updateDOM(id) {
    // updates the id element based on form state
}
</code></pre>

<p>should I invoke it via:</p>

<pre><code>window.onload = function() { updateDOM("myElement"); };
</code></pre>

<p>or:</p>

<pre><code>&lt;div id="myElement"&gt;...&lt;/div&gt;
&lt;script language="javascript"&gt;
    updateDOM("myElement");
&lt;/script&gt;
</code></pre>

<p>The former seems to be the standard way to do it, but the latter seems to be just as good, perhaps better since it will update the element as soon as the script is hit, and as long as it is placed after the element, I don't see a problem with it.</p>

<p>Any thoughts?  Is one version really better than the other?</p>

[{'answer_id': 40972, 'author': 'John Millikin', 'author_id': 3560, 'author_profile': 'https://Stackoverflow.com/users/3560', 'pm_score': 4, 'selected': True, 'text': "<p>Definitely use <code>onload</code>. Keep your scripts separate from your page, or you'll go mad trying to disentangle them later.</p>\n"}, {'answer_id': 40977, 'author': 'David Basarab', 'author_id': 2469, 'author_profile': 'https://Stackoverflow.com/users/2469', 'pm_score': 0, 'selected': False, 'text': '<p>My take is the former becauase you can only have 1 window.onload function, while inline script blocks you have an <em>n</em> number.</p>\n'}, {'answer_id': 40996, 'author': 'DancesWithBamboo', 'author_id': 1334, 'author_profile': 'https://Stackoverflow.com/users/1334', 'pm_score': 0, 'selected': False, 'text': '<p>onLoad because it is far easier to tell what code runs when the page loads up than having to read down through scads of html looking for script tags that might execute.</p>\n'}, {'answer_id': 41005, 'author': 'The How-To Geek', 'author_id': 291, 'author_profile': 'https://Stackoverflow.com/users/291', 'pm_score': 1, 'selected': False, 'text': "<p>While I agree with the others about using window.onload if possible for clean code, I'm pretty sure that window.onload will be called again when a user hits the back button in IE, but doesn't get called again in Firefox. (Unless they changed it recently). </p>\n\n<p>Edit: I could have that backwards.</p>\n\n<p>In some cases, it's necessary to use inline script when you want your script to be evaluated when the user hits the back button from another page, back to your page.</p>\n\n<p>Any corrections or additions to this answer are welcome... I'm not a javascript expert.</p>\n"}, {'answer_id': 41008, 'author': 'John Millikin', 'author_id': 3560, 'author_profile': 'https://Stackoverflow.com/users/3560', 'pm_score': 1, 'selected': False, 'text': "<p>@The Geek</p>\n\n<blockquote>\n  <p>I'm pretty sure that window.onload will be called again when a user hits the back button in IE, but doesn't get called again in Firefox. (Unless they changed it recently).</p>\n</blockquote>\n\n<p>In Firefox, <code>onload</code> is called when the DOM has finished loading regardless of how you navigated to a page.</p>\n"}, {'answer_id': 41011, 'author': 'matt b', 'author_id': 4249, 'author_profile': 'https://Stackoverflow.com/users/4249', 'pm_score': 2, 'selected': False, 'text': '<p>Some JavaScript frameworks, such as <a href="http://docs.mootools.net/Utilities/DomReady" rel="nofollow noreferrer">mootools</a>, give you access to a special event named "domready":</p>\n\n<blockquote>\n  <p>Contains the window Event \'domready\', which will execute when the DOM has loaded. To ensure that DOM elements exist when the code attempting to access them is executed, they should be placed within the \'domready\' event.</p>\n\n<pre><code>window.addEvent(\'domready\', function() {\n  alert("The DOM is ready.");\n});\n</code></pre>\n</blockquote>\n'}, {'answer_id': 41013, 'author': 'Neall', 'author_id': 619, 'author_profile': 'https://Stackoverflow.com/users/619', 'pm_score': 4, 'selected': False, 'text': "<p>The onload event is considered the proper way to do it, but if you don't mind using a javascript library, jQuery's $(document).ready() is even better.</p>\n\n<pre><code>$(document).ready(function(){\n  // manipulate the DOM all you want here\n});\n</code></pre>\n\n<p>The advantages are:</p>\n\n<ol>\n<li>Call $(document).ready() as many times as you want to register additional code to run - you can only set window.onload once.</li>\n<li>$(document).ready() actions happen as soon as the DOM is complete - window.onload has to wait for images and such.</li>\n</ol>\n\n<p>I hope I'm not becoming The Guy Who Suggests jQuery On Every JavaScript Question, but it really is great.</p>\n"}, {'answer_id': 41019, 'author': 'TonyLa', 'author_id': 1295, 'author_profile': 'https://Stackoverflow.com/users/1295', 'pm_score': 2, 'selected': False, 'text': '<p>window.onload on IE waits for the binary information to load also.  It isn\'t a strict definition of "when the DOM is loaded".  So there can be significant lag between when the page is perceived to be loaded and when your script gets fired.  Because of this I would recommend looking into one of the plentiful JS frameworks (prototype/jQuery) to handle the heavy lifting for you.</p>\n'}, {'answer_id': 41049, 'author': 'Ryan Doherty', 'author_id': 956, 'author_profile': 'https://Stackoverflow.com/users/956', 'pm_score': 3, 'selected': False, 'text': '<p>I\'ve written lots of Javascript and window.onload is a <strong>terrible</strong> way to do it. It is brittle and waits until <strong>every</strong> asset of the page has loaded. So if one image takes forever or a resource doesn\'t timeout until 30 seconds, your code will not run before the user can see/manipulate the page.</p>\n\n<p>Also, if another piece of Javascript decides to use window.onload = function() {}, your code will be blown away.</p>\n\n<p>The proper way to run your code when the page is ready is wait for the element you need to change is ready/available. Many JS libraries have this as built-in functionality. </p>\n\n<p>Check out:</p>\n\n<ul>\n<li><a href="http://docs.jquery.com/Events/ready#fn" rel="noreferrer">http://docs.jquery.com/Events/ready#fn</a></li>\n<li><a href="http://developer.yahoo.com/yui/event/#onavailable" rel="noreferrer">http://developer.yahoo.com/yui/event/#onavailable</a></li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/40966', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/122/']