Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-06-2007, 04:15 PM   PM User | #1
lore
New Coder

 
Join Date: Apr 2007
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
lore is an unknown quantity at this point
Problem calling function with onreadystatechange

Hello,
I am an AJAX beginner. I am seeing the following problem when I try to call a function with http_request.onreadystatechange.

Problenm description: I have the following simple HTML where I create a new MyForm object.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html lang="en" dir="ltr" >
<head>
<script src="http://lxplus012.cern.ch:1972/temporary/WebLib.js" ></script>
<link type="text/css" href="http://lxplus012.cern.ch:1972/temporary/style.css" rel="stylesheet"  />
<meta http-equiv="pragma" content="no-cache"  />
</head>
<script type="text/javascript" >
var myform = new MyForm('myform');
</script>
<h1 style="font-family: arial" >Elements Status</h1>
<p id="formsParId" >
</p>
<body onload="printList()" >
</body>
</html>

Then I have my WebLib.js javascript where I have several functions: one of them is the printList() function and the other is the AJAX function which uses onreadystatechange:

Code:
 function makeHttpRequest(url) 
{
  http_request = false;
  if (window.XMLHttpRequest) 
  { 
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType)
    {
      http_request.overrideMimeType('text/xml');
    }
  }
  if (!http_request) 
  {
    alert('Giving up :( Cannot create an XMLHTTP instance');
  }

  http_request.onreadystatechange = function() {printList();};
  http_request.open('GET', url, true);
  http_request.send(null);

  return;
}

////////////////////////////////
function MyForm(name){
  this.name=name;
 MyForm.list[name]=this
};
/////////////////////////////////////////////
  function printList(){
 	 alert("printList");
        for(var i in MyForm.list){
 	 alert("Found form i="+i);
        }
    }

My problem is that if I call printList() from my HTML using onload="printList()" then everything is fine and my MyForm.list if not 0 , i.e. I get the alert("Found form i="+i);
If I get rid of the call to printList() in HTML and I do it by calling makeHttpRequest(url)
(this is linked to a button on my web page) then it does not find anithing in MyForm.list, i.e. I only get to the first alert alert("printList"); Is there a way to seolve this without having to put "var myform = new MyForm('myform');" in my WebLib.js? Can anybody help?

Thanks in advance!
Lore
lore is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:22 AM.


Advertisement
Log in to turn off these ads.