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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:48:20+00:00 2026-05-20T21:48:20+00:00

I got this example demonstrating AJAX from Stoyan Stefanovs Object Oriented JavaScript at page

  • 0

I got this example demonstrating AJAX from Stoyan Stefanovs Object Oriented JavaScript at page 275. In this example, he is requesting three different files. I have a couple questions if any of you can help.

  1. What is xhr.send('') doing? Why do we need it? I thought GET in the line before was establishign contact with the server, so why this send?
    (The other questions relate to the closure that I don`t fully understand…)

  2. what exactly is getting passed as a paramater to function(myxhr)?

  3. regarding the anonymous function, which has (xhr) passed as a parameter, can you explain at what point in the program xhr gets passed to the anonymous function? For example, is it after xhr.open has taken place?

  4. why is function(myxhr) necessary? If it`s to create closure, why is closure necessary here?

  5. is parameter (xhr) of the anonymous function getting passed as parameter myxhr in function(myxhr) once the anonymous function is called?

  6. if 5 is true–that xhr is passed as a parameter to function(myxhr)–why is it necessary to change the parameter name from xhr to myxhr?

Sample code:

function request(url, callback){
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = (function(myxhr){
    return function() {
      callback(myxhr);
    }
  })(xhr);
  xhr.open('GET', url, true);
  xhr.send('');
}

request (
  'http://www.phpied.com/files/jsoop/content.txt',
  function (o){
    document.getElementById('text').innerHTML = o.responseText;
  }
);

request(
  'http://www.phpied.com/files/jsoop/content.html',
  function(o) {
    document.getElementById('html').innerHTML = o.responseText;
  }
);

request(
  'http://www.phpied.com/files/jsoop/content.xml',
  function(o){
    document.getElementById('xml').innerHTML =
    o.responseXML.getElementsByTagName('root')[0].firstChild.nodeValue;
  }
);
  • 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-20T21:48:20+00:00Added an answer on May 20, 2026 at 9:48 pm

    What is xhr.send(”) doing? Why do we
    need it? I thought GET in the line
    before was establishign contact with
    the server, so why this send? (The
    other questions relate to the closure
    that I don`t fully understand…)

    Open just sets up the request. Send actually sends it. When you make a POST request you need to pass the to send.

    what exactly is getting passed as a
    paramater to function(myxhr)?

    The content of the variable xhr.

    regarding the anonymous function,
    which has (xhr) passed as a parameter,
    can you explain at what point in the
    program xhr gets passed to the
    anonymous function? For example, is it
    after xhr.open has taken place?

    As soon as the function is called (which is immediately as the definition is followed by ()).

    why is function(myxhr) necessary? If
    it`s to create closure, why is closure
    necessary here?

    It isn’t. Even if the xhr variable wasn’t locally scoped to the request function (which it is) then it could be accessed via this.

    is parameter (xhr) of the anonymous
    function getting passed as parameter
    myxhr in function(myxhr) once the
    anonymous function is called?

    Yes.

    if 5 is true–that xhr is passed as a
    parameter to function(myxhr)–why is
    it necessary to change the parameter
    name from xhr to myxhr?

    It isn’t. You can reuse the variable name at different scopes. It makes it less confusing though.

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

Sidebar

Related Questions

I got this example from the php site: <?php try { $dbh = new
Ok i got this example from Regular Expression Cookbook ^(?=.{3}$).* The regex above is
thank you for looking i got this example from my book but i cant
I've got some XML, for example purposes it looks like this: <root> <field1>test</field1> <f2>t2</f2>
I got this síngleton cache object and it exposes an IEnumerable property which just
I've got this example where I convert a C# 2 delegate example: Predicate<string> predicate2
how to use LIKE in CASE statement, I have got this example script but
I got this error today when trying to open a Visual Studio 2008 project
I got this output when running sudo cpan Scalar::Util::Numeric jmm@freekbox:~/bfwsandbox/sa/angel/astroportal/dtu8e/resources$ sudo cpan Scalar::Util::Numeric [sudo]
I got this flash application where you can click a link while watching a

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.