...

View Full Version : problem finding new object when calling function in my Javascrip file



lore
06-07-2007, 02:09 PM
Hi,
I posted this request for help on the AJAX forum but now I discovered the problem does not have anything to do with AJAX. I re-post it here:

Hello,
I am not a Javascript expert and if possible I would like to have some help with this problem. I have the following simple HTML where I create a new MyForm object.



<!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 called inthe HTML. The other is makeHttpRequest() which is called when i press a button on my web page. Here a snippet of my .js file:




function makeHttpRequest()
{
printList(); // I simplified this function (where AJAX is used) to only call printList()
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 is not 0 , i.e. I get to the line alert("Found form i="+i);

If I get rid of the call to printList() in HTML above and I call printList() inside the function makeHttpRequest(), then it does not find anithing in MyForm.list, i.e. I only get to the
first line alert("printList") in printList(); Does anybody know why? Is there a way to solve this keeping the declaration "var myform = new MyForm('myform');" in the HTML ?
Can anybody help please?

Thanks in advance!
Lore

cyberlogi
06-07-2007, 06:30 PM
Your variable 'MyForm' is a Function not an Object. It looks like your 'MyForm' function is meant to create a scoped object, but you never set it to anything. You need to initialize your form with a method like:


var foo = new MyForm(name);

However, if you don't need all the power of a scoped object, but just need the HTMLFormElement Object, then I suggest you scrap the 'MyForm' function altogether.

First create a variable 'MyForm' in the global scope:


var MyForm = null;

Then in your onload event callback set MyForm equal to your HTMLFormElement Object with:


var myOnLoad() = function() {
MyForm = document.getElementById('MyFormId');
};

Now any function can use 'MyForm' and will find it has a value. If you don't already have, I suggest you get the 'FireBug' extension for FireFox, which allows you to step through your JavaScript code and see what values variables have (as well as a whole lot more).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum