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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:12:08+00:00 2026-06-18T04:12:08+00:00

var mapFile = new XMLHttpRequest(); mapFile.open(GET, http://localhost:8000/res/map01.txt, true); mapFile.onreadystatechange = function() { if (mapFile.readyState

  • 0
var mapFile = new XMLHttpRequest();
mapFile.open("GET", "http://localhost:8000/res/map01.txt", true);

mapFile.onreadystatechange = function() {
  if (mapFile.readyState === 4) {
    if (mapFile.status === 200) {
      this.lines = mapFile.responseText.split("\n");
    }
  }
}

this.lines = mapFile.onreadystatechange.lines;

mapFile.send(null);

I have that code and I’m trying to save this.lines inside mapFile.onreadstatechange to later save as this.lines on the outer scope. However, mapFile.onreadystatachange.lines is undefined and I can’t save the variable for later use. I even tried using element.innerHTML which is a dirty hack for this but it also didn’t work.

  • 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-18T04:12:09+00:00Added an answer on June 18, 2026 at 4:12 am

    There are three primary issues here:

    1. The this within onreadystatechange is not the same as the this outside of the function, because this within a function is determined by how the function is called. More on my blog: Mythical methods | You must remember this

    2. XHR calls are asynchronous (by default), so your callback won’t have been called yet when you’re trying to use this.lines just above the send call. (Even with a synchronous request, since that line is before send, it still wouldn’t be set.)

    3. mapFile.onreadystatechange.lines looks for a property called lines on the function object referenced by mapFile.onreadystatechange. It has absolutely nothing to do with any variables defined within that function.

    Here’s an update addressing the main items:

    var mapFile = new XMLHttpRequest();
    var self = this; // <===== Remember `this` for use in the callback
    mapFile.open("GET", "http://localhost:8000/res/map01.txt", true);
    
    mapFile.onreadystatechange = function() {
      if (mapFile.readyState === 4) {
        if (mapFile.status === 200) {
          // v---- Use self instead of this
          self.lines = mapFile.responseText.split("\n");
        }
      }
    }; // <=== Add missing semicolon
    
    // Removed a line here using `this.lines` prematurely
    
    mapFile.send(null);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

var ref1 = new Firebase(http://gamma.firebase.com/myuser/123,456); ref1.set(123,456); var on1 = ref1.on(value, function(snapshot) { console.log(snapshot.val()); });
Following code working: function() { var Marker = new google.maps.Marker(); var MarkerOptions = {};
var geocoder, map, point, fmtAdd, marker; function mapLoad() { geocoder = new google.maps.Geocoder(); var
var htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlText), styles); document.Open(); BaseFont Vn_Helvetica = BaseFont.CreateFont(@C:\Windows\Fonts\arial.ttf, Identity-H, BaseFont.EMBEDDED); Font
var http = require('http'); var options = { method: 'GET', host: 'www.google.com', port: 80,
var Foo = Foo || {}; Foo.Controller = (function ($) { var $page =
var JavascriptHelper = Backbone.Model.extend(JavascriptHelper, {}, // never initialized as an instance { myFn: function()
var messageSpan; function createFlashMessage = function(message, type, time) { messageSpan || (messageSpan = $(<span>).hide().appendTo(body));
I have a TileCache server with this configuration: [cache] type=Disk base=/var/maps/cache [osm] type=Mapnik mapfile=/var/maps/bin/mapnik/osm.xml
var stage = new Kinetic.Stage({ container: 'container', width: 578, height: 200 }); var layer

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.