Note that there are some explanatory texts on larger screens.

plurals
  1. POHow should I load/unload JS called by dynamically loaded partial pages?
    primarykey
    data
    text
    <p>I make use of <em>jQuery</em> and <em>history.js</em> to manage dynamic transitions between partial-pages; such that I avoid reloading entire documents. Some of these partial-pages call their own unique <em>javascript</em> files. While the transitions between pages work well, remnants of executed <em>javascript</em> remain active after the partial page that called it has been dynamically replaced.</p> <p><strong>How can I unload <em>javascript</em> that was introduced with a dynamic page load, and later asynchronously replaced by another page?</strong></p> <p><br></p> <h1><em>The finer details</em></h1> <h2>Master template</h2> <p>My <strong>master template</strong> (used for all pages) can be thought of as a simple:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script&gt; //scripts to manage asynchronous loading of partial-pages into #content div &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="content"&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <h2>User profile</h2> <p>One partial page that renders inside the <strong>#content</strong> div is for a user's <strong>Profile</strong>:</p> <pre><code>&lt;script src="profile.js"&gt;&lt;/script&gt; &lt;form&gt; &lt;input type="file" name="profile-picture"&gt; &lt;/form&gt; </code></pre> <p>The contents of <strong>profile.js</strong> are similar to:</p> <pre><code>$(function() { $('input').change(function() { // upload profile picture asynchronously }); }); </code></pre> <h2>User settings</h2> <p>Another partial page that is loaded inside the <strong>#content</strong> div of the <strong>master template</strong> is the user's <strong>Settings</strong>:</p> <pre><code>&lt;script src="settings.js"&gt;&lt;/script&gt; &lt;form&gt; &lt;input type="text" name="first-name"&gt; &lt;input type="text" name="last-name"&gt; &lt;/form&gt; </code></pre> <p>The contents of <strong>settings.js</strong> are similar to:</p> <pre><code>$(function() { setInterval(function() { // auto-submit form every 10 seconds $('form').submit(); }, 10000); } }); </code></pre> <h2>The problems</h2> <ul> <li>Certain <em>javascript</em> functions continue to run (e.g. <strong>setInterval</strong>) after the partial page that called them has been replaced by another.</li> <li>This business of loading new <em>javascript</em> for each partial page feels messy; but for the life of me, I can't find any recommendations for best practices.</li> </ul> <p><strong>What is the better way to achieve this effect of dynamic partial-page loading/unloading while allowing page-specific scripts for each partial page?</strong></p> <p>Thank you!</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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