Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When dynamically loading javascript the easiest way is to attach it as a script element (as suggested by Ben Carey in comments above). Here is the code in normal javascript rather than relying on JQuery:</p> <pre><code>function loadJs(file) { var head= document.getElementsByTagName('head')[0], script=document.createElement('script'); script.setAttribute("type","text/javascript"); script.setAttribute("src", file); head.appendChild(script); } </code></pre> <p><strong>EDIT:</strong> You can call this function at the point you want the script to be loaded. It is probably best done immediately before the ajax call so that both requests can run concurrently:</p> <pre><code>loadJs('runscript2.js'); if (window.XMLHttpRequest) ... </code></pre> <p>However, having looked at your question a little more carefully, there is still a problem. The script may load before you have updated the dom with the new form. This will prevent your script working as it assumes the dom is there before it is called. You could solve this by only calling loadJS once the dom has been updated but this would result in an unnecessary delay before the form was functional. The alternative is to change runscript.js so that it reads:</p> <pre><code>&lt;script id=runscript2 type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/ext-core/3/ext-core.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" id="runscript"&gt; var runscript2 = function() { Ext.fly('form').on('submit', function(e){ ... }; &lt;/script&gt; </code></pre> <p>you can then call the function runscript2 after you have manipulated the dom (not shown in your code snippets but presumably looks something like this)</p> <pre><code>document.getElementById('someDiv').innerHTML=xmlhttp2.responseText; var runscript2Ready = function() { if (typeof runscript2 === 'function') { runscript2(); } else { setTimeout(runscript2Ready, 50); } } runscript2Ready(); </code></pre> <p>Here, after the dom has been updated, we run a function which executes the javascript if it has been loaded or, if not, keeps trying again at 50ms intervals until it has.</p> <p><strong>Note:</strong> If I was cleverer, I would have said this at the start, but do you actually need to dynamically load the js? Why not just include it in the original index file (modified as I've suggested) and then just call the runscript2 function after manipulating the dom. That way you don't need to worry if the js has loaded yet.</p> <p><strong>Note2:</strong> If I was even cleverer I would know something about extJS (which it appears you are using) but, unfortunately, I don't. It is possible (but unlikely) that the Ext.onReady method does something more special than simply waiting for the dom to load. In this case my code will fail. More likely is that ExtJS has the facility to deal with dom elements that don't yet exist (as JQuery does). This would simplify your situation enormously.</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.
    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