I have tried 3 suggestions found in other questions on the site but none seem to work for me and I'm not sure why.
// #1
var imported = document.createElement('script');
imported.src = '/path/to/imported/script';
document.getElementsByTagName('head')[0].appendChild(imported);
// #2
// jQuery
$.getScript('/path/to/imported/script.js', function()
{
// script is now loaded and executed.
// put your dependent JS here.
});
// #3
document.write( '<script language="javascript" src="myotherscript.js" />' );
The file I am trying to include is basically a massive file with variables declared like so:
var agent = [
"csv",
"drawfile",
"excel",
"flash",
"hangul",
"html",
"image",
"msword",
"ooxml",
"pdf",
"ppt",
"txt",
"wmf"];
Any ideas on what could be causing the problem?
UPDATE
Just an update to say that the JS file that I'm trying to include is generated dynamically from a database, so avoid regular expressions to add and remove the same bit of code constantly I have it stored in a serparate file.
The variables concerend are used to populate a dynamic drop down list with the values, so by not working I mean no values in the dropdowns :(
<script>tag, like any other script module?var. The real question is, if your file is just a bunch of data, why don't you just use JSON to store it, and then parse the json in your main code? No need to dynamically load & run javascript to initialize some variables.<script ... />won't work in browsers that don't support XHTML (like IE), and if you're using a non-XHTML doctype it won't work anyway. (HTML, even HTML5, sees it as an opening tag with no closing tag.)