I have a simple question: Can you init a function in the callback of a $.get()?
For example, this doesn’t alert “get”:
function outside() {
alert('get');
}
$.get('foo.txt', function(data) {
outside();
});
However, using $.ajax alerts “ajax” as expected:
function outside() {
alert('ajax');
}
$.ajax({
url: 'foo.txt',
type: 'GET',
success: function(data) {
outside();
}
});
I realize that $.get() is just $.ajax() stripped, but how am I supposed to know whether I can use an outside function in a callback?
Edit: Close this, it is a personal mistake.
The file I was calling had a header('Content-type: application/xml');, so the “intelligent” data-type set by jQuery was “xml” when it should have been set to “html.” My code works with $.get() when you use:
function outside() {
alert('get');
}
$.get('foo.txt', function(data) {
outside();
},
"html"
);
Yes, you can. Your jsfiddle fails because the request for ‘foo.txt’ returns an error. Here is an example of a working jsfiddle: http://jsfiddle.net/pXSmj/
Here is an update to your jsfiddle with
outsidebeing called to the failure of the request for foo.txt: http://jsfiddle.net/d69vz/1/