Is it possible to send a variable to the script loaded using $.getScript?
At the moment I have:
$.getScript( "js/my_script.js", function() {
// do something here after script has loaded
});
But I want to send a variable to the loaded script. Is this possible, if yes, how?
For example
// this is a variable set outside of the script loaded via `$.getScript`
var my_variable = 1
// how do I get that variable sent to `my_script.js`
$.getScript( "my_script.js", function() {
// do something here after script has loaded
});
jQuery.getScript():
From the documentation, it seems that your variable should be accessible within a
$.getScript(function(){..})call. Therefore, this is likely a problem with scope. Your variablemy_variableexists (presumably) inside$(document).ready(function(){...}), and is therefore restricted to that particular scope.Try using a global variable by assigning your data to the window object:
Sources: old question here and $.getScript docs here.
Verified on my personal server.