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 8191647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:57:50+00:00 2026-06-07T03:57:50+00:00

Latey I’ve got some trouble with some weird javascript behavior. I want to do

  • 0

Latey I’ve got some trouble with some weird javascript behavior. I want to do something like this:

var lang = null;

function getLang() {
    if (browserLanguageIsGerman) {
        lang = 'de';
    }
    else {
        lang = 'en';
    }
    // alert(lang) shows "de"

    $('#someID').load(someValidUrl, null, 
        function(response, status, xhr) {
            if(languageSettingsOnFacebookIsGerman) {
                lang = 'de';
            }
            else {
                lang = 'en';
            }
            // alert(lang) show "en"
    );
    // alert(lang) shows "de"
}

The first and the second alerts show the expacted value 1) “de” 2) “en”. The third alert shows “de” but shouldn’t it be “en”?!
Also the second alert pops up after the third alert.

Can someone please obvious bug in my mind? 🙂

Thanks in advance!

  • 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-07T03:57:53+00:00Added an answer on June 7, 2026 at 3:57 am

    This is not an issue with scope. The load method is asynchronous. The third alert is executed before the callback you pass to load. Move any code that depends on the result of that async call into the callback.

    Alternatively, you can look into the jQuery deferred objects API. Note that if you were to use the deferred object API you would need to change you call to load to a call to jQuery.get or jQuery.ajax, since .load returns an instance of jQuery, which doesn’t implement the Promise interface.

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

Sidebar

Related Questions

Lately I came across some weird behavior with the defined? operator used to check
Lately something weird has been happening to my projects in xcode: I've been trying
Lately, I've gotten some weird linker errors. I've been taught that there's two ways
Lately I've been writing some JS code using jQuery and JavaScript as it is
Lately, we've been seeing exceptions like this in our .NET (.asmx) webservices: System.Web.Services.Protocols.SoapException: Server
Lately, I've often found myself wanting to run some command on the output of
Lately, I saw the following line: [someObject release], someObject = nil; Why does this
Lately I had an issue with an array that contained some hundred thousands of
Lately I've been running into some subtle layout issues in my iOS app. For
Late Edit I tagged this as a C# question as well as C++ because

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.