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

  • SEARCH
  • Home
  • 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 7513949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:20:45+00:00 2026-05-30T00:20:45+00:00

If you look at the function below, on line 11 where it alert(template); .

  • 0

If you look at the function below, on line 11 where it alert(template);. It prints undefined. If I alert(template); inside the ajax success callback, it prints no problem. Since template is defined at the top of the function, shouldn’t this be global through out the entire function? Can someone assist me with this?

function load_template(path, data, callback){
    var template;

    $.ajax({
        url: path,
        success: function(uncompiled_template){
            template = Handlebars.compile(uncompiled_template);
        }
    });

    alert(template);    

    if(callback){
        callback(template, data);
    }else{
        return template(data);
    }
}
  • 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-05-30T00:20:46+00:00Added an answer on May 30, 2026 at 12:20 am

    No, it’s not a scope issue, it’s a timing issue.

    The AJAX call is asynchronous, so the success callback runs later, when the response arrives. The variable is simply not set yet, when you try to use it.

    You can’t return the value from the function, as the success callback never runs until you have exited the function. Always use the callback that is sent to the function:

    function load_template(path, data, callback){
    
        $.ajax({
            url: path,
            success: function(uncompiled_template){
                var template = Handlebars.compile(uncompiled_template);
                callback(template, data);
            }
        });
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Look at this: $('form).submit(function (event) { $(':input.required').trigger('blur'); var num = $('.warning', this).length; alert(num);//Focus here!
I need to reformat the function below in the form of an jQuery.ajax call
Take a look at this snippet below: <script> $(function(){ var to_username = alex; window.Persona
Suppose i have a function template StrCompare template<typename T=NonCaseSenCompare>//NonCaseSenCompare is a user defined class
If you take a look at the clock_gettime() function, which is available in all
I've got a function here that is meant to look through a list of
take a look at this code: $(document).ready(function() { document.getElementById(sliderId).onmousedown = sliderMouseDown; }); function sliderMouseDown()
look at this code, <head> <meta http-equiv=Content-Type content=text/html; charset=utf-8 /> <script> function change() {
I am working on a helper macro that look into the list function on
Take a look at this html: <head> <title>Test page</title> <script type=text/javascript> function submitForm() {

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.