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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:06:42+00:00 2026-05-23T12:06:42+00:00

I have this code: … function init() { getdata(); } var txt; function getdata()

  • 0

I have this code:

...

  function init() {
      getdata();
  }

  var txt;
  function getdata() {
      var xhr = new XMLHttpRequest();
      xhr.open('GET',myurl, true);
      xhr.setRequestHeader('Cache-Control', 'no-cache');
      xhr.setRequestHeader('Pragma', 'no-cache');
      xhr.onreadystatechange = function() {
          if (xhr.readyState == 4) if (xhr.responseText) {
              txt = xhr.responseText;
          }
      }

      xhr.send();
      console.log(txt);
  }

...

<body onload="init();">

...

Why can’t I get txt value? Please help.
forgot to mention – all of this happens in background page, i dont have any other pages at the moment. i tried console via extensions overview for background page, but that console doesnt output anything.. :((

  • 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-23T12:06:43+00:00Added an answer on May 23, 2026 at 12:06 pm

    The XHR request is asynchronous, so when you log txt to console immediately after sending it, the request hasn’t completed yet so txt is undefined.

    Either set it be run synchronously by setting async to false in the open command, or just stick console.log(txt) in the function you have bound to the onreadystatechange event already.

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

Sidebar

Related Questions

I have this code somewhere in my plugin: $('tr', $this).hover(function(){ var cur_color = null;
I have this code: require(class.XMLHttpRequest.php); function hot($news){ $url=https://localhost/search.aspx?search=.$news.; $ajax=new XMLHttpRequest(); $ajax->setRequestHeader(Cookie,Cookie: host); $ajax->open(GET,$url,true); $ajax->send(null);
I have this code var stats = { GetMetaData : function() { var url
I have this code fragment var combo = new dhtmlXCombo(combo_zone4, alfa4, 230); combo.loadXML(create_store2.php); combo.attachEvent(onChange,
I have this code var contacts = dr.mktDoctorContacts .GroupBy(x => x.ContactType) .Select(zb => new
I have this code & I've tired using JQuery's appendTo() function to get this
I have this code :- using (System.Security.Cryptography.SHA256 sha2 = new System.Security.Cryptography.SHA256Managed()) { .. }
I have this code: if (PsionTeklogix.Keyboard.Keyboard.GetModifierKeyState(Key.Orange) == KeyState.Lock) PsionTeklogix.Keyboard.Keyboard.InjectKeyboardCommand(Function.Orange, 0, 0); if (PsionTeklogix.Keyboard.Keyboard.GetModifierKeyState(Key.Blue) ==
I have this code : var proxy = XmlRpcProxyGen.Create<IMessageRpc>(); var proxy2 = XmlRpcProxyGen.Create<ITestRpc>(); Create
I have this code guile> (cairo-pdf-surface-create foo.pdf 100.0 100.0) ; and get this error

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.