This is a very simple script that should load jQuery. I can see in the Firebug Scripts tab that jquery is loading but I get '$ is not defined" errors when I try to use it. Can anyone help me understand what's wrong?
//function to add scripts
function include(file)
{
var script = document.createElement('script');
script.src = file;
script.type = 'text/javascript';
script.defer = true;
document.getElementsByTagName('head').item(0).appendChild(script);
}
//add jQuery
include('https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');
//See if jQuery is working
$(document).ready(function() {
$('#internal').show();
})
////////////
//RETURNS: "$ is not defined $(document).ready(function() {"
The odd thing is if don't try to use jQuery in this same script instead I load another js file that uses jQuery it does work
//function to add scripts
function include(file)
{
var script = document.createElement('script');
script.src = file;
script.type = 'text/javascript';
script.defer = true;
document.getElementsByTagName('head').item(0).appendChild(script);
}
//add jQuery
include('https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');
//add my custom script that wants to use jQuery
include('scripts/testScript.js')
testScript.js
$(document).ready(function() {
$('#external').show();
})
I appreciate any advice with this.