Hey all,
After reading
a particular post I came up with some code to load javascript files dynamically. But I've got problems..
When the script element is dynamically added to the head section of html, i think that the document.ready event fires once again and therefore the code sort of runs twice.
In the html page I call this method:
function TestLoad()
{
$(document).ready( function() {
alert('begin');
//this line is causing the problem
$("script").appendTo("head").attr({src:'/js/test.js', type: 'text/javascript'});
$.getScript("/js/test.js", function(){
//wait before initialising to prevent intermittent load error
alert('getScript');
setTimeout("SayHi", 250);
});
});
}
In the script test.js I have the function SayHi():
function SayHi()
{
alert('Hi!');
}
The SayHi method never gets called and alert('begin') & alert('getScript') get called twice in this sequence:
begin begin getScript getScript
What's wrong with my code?
Aaron