Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AJAX Basics (retiring) Programming AJAX Create a callback

Julianna Kahn
Julianna Kahn
20,702 Points

I don't understand what is meant by assigning an "anonymous" function.

Maybe I am a little confused on my terminology? The function is the same as the event handler?

app.js
var xhr = new XMLHttpRequest();
xhr.onReadyStateChange = function() {
if(xhr.ReadyState === 4);
alert(xhr.statusText);
}
xhr.open('GET', 'sidebar.html');
xhr.send();
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>AJAX with JavaScript</title>
  <script src="app.js"></script>
</head>
<body>
  <div id="main">
    <h1>AJAX!</h1>
  </div>
  <div id="sidebar"></div>
</body>
</html>

4 Answers

Steven Parker
Steven Parker
231,172 Points

The "anonymous" part just means that the function is created in an expression where it is not given a name. It doesn't need one since the code has another way to reference it.

The instructions say to "add an onreadystatechange event handler.", You did add an event handler, but you assigned it to a property named "onReadyStateChange" (with mixed case) instead of "onreadystatechange" (all lower case).

Also, while it won't affect passing, the instructions also said to add the function "but don't put any code inside it."

And this will also be ignored by this challenge, but just FYI: for an "if" to work, you can't put a semicolon after the conditional clause.

Julianna Kahn
Julianna Kahn
20,702 Points

I thought that would mean function().

Steven Parker
Steven Parker
231,172 Points

That's exactly what you have on the second line:

xhr.onReadyStateChange = function() {  // <- anonymous function
Julianna Kahn
Julianna Kahn
20,702 Points

I thought before that it might be but I still get an error:

Bummer: There is no event handler for the onreadystatechange event. You can set the event handler by assigning an anonymous function to xhr.onreadystatechange.

var xhr = new XMLHttpRequest(); xhr.onReadyStateChange = function() { } xhr.open('GET', 'sidebar.html'); xhr.send();

Steven Parker
Steven Parker
231,172 Points

Oh, I thought the question was just about anonymous functions in general. I expanded my original answer. :arrow_heading_up:

Does that help?

Julianna Kahn
Julianna Kahn
20,702 Points

Yes, your explanation helps a lot. The if statement was just something I was trying to test if that was why I was getting the error, but I guess not. And I understand that the semicolon didn't help.