Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8954609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:20:42+00:00 2026-06-15T14:20:42+00:00

I have a simple question: Can you init a function in the callback of

  • 0

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();
});

http://jsfiddle.net/d69vz/3/

However, using $.ajax alerts “ajax” as expected:

function outside() {
    alert('ajax');
}
$.ajax({
    url: 'foo.txt',
    type: 'GET',
    success: function(data) {
        outside();
    }
});

http://jsfiddle.net/v8zqZ/

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"
);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:20:44+00:00Added an answer on June 15, 2026 at 2:20 pm

    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/

    var speak = function() { alert('hi'); }
    
    $(document).ready(function() {
        $.get('../img/logo.png', function(data) {
            speak();
        });
    });​
    

    Here is an update to your jsfiddle with outside being called to the failure of the request for foo.txt: http://jsfiddle.net/d69vz/1/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple question, How can I get the POST values from
I have what I think is a simple question but I can't get it
I have a simple question. Can a grid automatically resize the objects in it
Simple question which I can't seem to find an answer of: I have two
I have a very simple question for which I can't seem to find an
Have a rather simple question. Does anyone knows if i can use jparallax both
A fairly simple question for which I have a guess, but I can't find
I've a simple question. Can a subclass have a private constructor (i.e. for singleton
I have what is undoubtedly a simple question, but I can't seem to find
simple problem with a simple question. I have a string, in C# i can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.