Simple javascript onstatechange help.

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Vael Victus
Forum Newbie
Posts: 24
Joined: Wed Jun 03, 2009 9:29 am

Simple javascript onstatechange help.

Post by Vael Victus »

Hi, I know this is like so simple but I can't figure it out. >____> I'm making AJAX.

All I want to do is check if the readystate is == 0, then display one set of code. (by a simple document.write) THEN since it's going to change when the user hits the button, it will display the other code. This is what I've got at the top.

Code: Select all

 
<script type="text/javascript">   
      var xmlhttp;
 
function showUser(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url=str;
//url=url+"?id="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
 
function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
 
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}
</script>
 
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Simple javascript onstatechange help.

Post by kaszu »

I didn't understood what you need. Please be more specific
Vael Victus
Forum Newbie
Posts: 24
Joined: Wed Jun 03, 2009 9:29 am

Re: Simple javascript onstatechange help.

Post by Vael Victus »

Okay. The code above is all the javascript for AJAX. I want to make an if statement for this code: (or at least I think so)

Code: Select all

 
# function stateChanged()
# {
# if (xmlhttp.readyState==4)
# {
# document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
# }
# }
 
I need to make an if statement to just see if no data has been submitted by the user. When I try, for example, if (xmlhttp.readyState==0), it doesn't work.
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Simple javascript onstatechange help.

Post by Reviresco »

Try three equal signs:

Code: Select all

if (xmlhttp.readyState === 0)
Not sure if that will help -- 0 means "The object has been created, but not initialized (the open method has not been called)" which of course will always be the case before it loads.
Post Reply