Reputation: 20223
I have this function:
$("#loading_button").click(function(){
(function() {
var s = document.createElement("script");
s.type = "text/javascript";
s.async = true;
s.src = '//some_src/';
var x = document.getElementsByTagName('script')[0];
s.onload = function() {
alert("OK");
$('#my_button').trigger('click');
};
x.parentNode.insertBefore(s, x);
})();
});
With the alert there, all works fine. #my_button from the src script is clicked automatically. The problem is that as soon as I remove the alert(), trigger, click trigger is not called anymore.
Any idea why?
Upvotes: 0
Views: 36
Reputation: 838
try not to call your code directly, but bind it to document.ready instead;
$(document).ready(function(){ // <-- look here
var s = document.createElement("script");
s.type = "text/javascript";
s.async = true;
s.src = '//some_src/';
var x = document.getElementsByTagName('script')[0];
s.onload = function() {
alert("OK");
$('#my_button').trigger('click');
};
x.parentNode.insertBefore(s, x);
}); // <-- and here
Upvotes: 2