readyState never gets to 4 on IE

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

I use this same basic layout for AJAX stuff and for some reason, I can't get it to work this time on IE (works fine in Opera and Firefox).

function getLessonPage(direction)
{
var http = false;
if(navigator.appName == "Microsoft Internet Explorer")
http = new ActiveXObject("Microsoft.XMLHTTP");
else
http = new XMLHttpRequest();
http.abort();
http.open("GET", "/courseware/ajax-lesson.php?direction=" + direction, true);
http.onreadystatechange=function()
{
if(http.readyState == 4)
{
lesson = http.responseText;
document.getElementById("lesson").innerHTML = lesson
}

alert(http.readyState);
}
http.send(null);
}
'

When I run this code in IE, I get alerts for 1, 2, 3 and then it stops. Not sure what's going on. This is the only JavaScript on the page.

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

In IE's little debugging thing it says:

Quote:
Line: 15
Char: 4
Error: Object doesn't support this property or method
Code:0
...

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

Nevermind. I just read that if I change the variable name (in this case, "lesson") it solves the problem. I changed these two lines:

lesson2 = http.responseText;
document.getElementById("lesson").innerHTML = lesson2;

I wonder why calling it something else makes it work in IE...

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

"lesson" was probably a reserved keyword.

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

I got the impression that it was because the variable name was the same as the element id. 'lesson' might well be reserved though, which was it Matt?

a Padded Cell our articles site!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.