Could someone please explain the behaviour of jQuery's getScript()
function?
Consider a javascript file test.js
:
var tmp = 'a variable';
alert('here');
When test.js
is loaded via html's <script>
tag, everything works fine: tmp
variable is available in the global scope and a message box appears.
I'm trying to get the similar behavior via this code:
<script>
$(document).ready(function() {
$.getScript("static/js/proto/test.js");
setTimeout(function() {
// at this point tmp should be available
// in the global scope
alert(tmp);
} , 2000); // 2 seconds timeout
}
</script>
But browser's error console reports an "Undefined variable tmp" error. What am I doing wrong? Thank you.