Forum Moderators: open
Thanks in advance to help you can give.
var j=document.createElement('script');
j.type='text/javascript';
j.src='http://example.com/js/yourJSfile.js';
j.id='scriptappended';
with(document.getElementsByTagName('head')[0]){
appendChild(j);
removeChild(j); // this line only if you don't want to leave a trace in the DOM tree
}
The content of the loaded javascript file will be executed as soon as the file has been loaded by the browser.
You can add an .onload attribute to the "j" object if you need your main script to be notified of the loading.
<script type="text/javascript" src="yourScriptFile.js"></script>
You need at least one <script> tag in you document to execute some javascript code.
If you want to include other javascript files at "runtime" without having to modify your html source code, the example code I given above is the solution.
function include(f,ol) {
var j=document.createElement('script');
j.type='text/javascript';
j.src=f;
if (ol) j.onload=ol;
document.getElementsByTagName('head')[0].appendChild(j);
}
where "ol" is the javascript code you want to execute after the include has completed
Now, if what you want is to include some html code from another page, the solution is probably ajax.
[edited by: Achernar at 9:23 pm (utc) on Oct. 17, 2007]