Note that there are some explanatory texts on larger screens.

plurals
  1. POBest practice for organizing JavaScript coming back from Ajax calls
    primarykey
    data
    text
    <p>I am writing an application (Asp.Net MVC) that contains a lot of reusable components. </p> <p>I am currently loading these components with Ajax calls, using the jQuery library of course.</p> <p>These components will be calling other sub-components as well, and so there are all these JavaScript coming back.</p> <p>So question is, <strong>where</strong> should I put the "callback" JavaScript? </p> <p>I currently have something like this:</p> <p><strong>The page:</strong></p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;blah&lt;/title&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" &gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="Container"&gt;&lt;/div&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $('#Container').load('SomeCrazyDomain.com/SomeComponent'); }); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>The Component:</strong></p> <pre><code>&lt;p&gt;Load sub components&lt;/p&gt;&lt;input type="button" onclick="GetSubcompent" /&gt; &lt;div id="SubcompentContainer"&gt;&lt;/div&gt; &lt;!-- Is this Bad? --&gt; &lt;script type="text/javascript"&gt; function GetSubcompent() { $('#SubcompentContainer').load('SomeCrazyDomain.com/SomeSubComponent'); } &lt;/script&gt; </code></pre> <p><strong>The sub component:</strong></p> <pre><code>&lt;h1&gt;I am a sub compent&lt;/h1&gt; &lt;script type="text/javascript"&gt; function ToBeLoadedForEachAjaxCall() { //do something } &lt;/script&gt; </code></pre> <p>If, I leave it as it is, what's the effect of loading ToBeLoadedForEachAjaxCall() for each ajax call? Will the broswers kill the last ToBeLoadedForEachAjaxCall() and replace with the "newer" one?</p> <p>Thank you,</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload