Not defined function from $.getScript
The issue is that the $.getScript()
function is asynchronous. When you call the Hello()
function immediately after, the script is not yet loaded so the function is not available.
Loading scripts with regular <script>
tags happens synchronously, so if you want to duplicate that behavior you have to disable the async
option in your Ajax call.
getScript
alone does not support this, so you can do this using an $.ajax
call with the appropriate options:
$.ajax({
url: 'myscript.js',
dataType: 'script',
async: false
});
This will block the browser until the script is loaded.
However, a better technique is to use a callback, which $.getScript()
does support:
$.getScript('myscript.js', function() {
Hello();
});
You need to wait for the response:
$.getScript('myscript.js', function(){
Hello();
});