Click to See Complete Forum and Search --> : How do I get a generic reference to the current form?
JSchwarz
06-10-2003, 07:39 AM
I have JavaScript.js referred to in a form's header via the following:<SCRIPT LANGUAGE='JavaScript' SRC='/DBName/JavaScriptLib.js/'></SCRIPT>
In JavaScript.js is a function called initialize(), which has the following line:thisForm = document.forms[0];I call initialize() from the form's onLoad event. This has worked great for obtaining a general reference to the current form for ALL of the forms in my application... until now.
Now I have a page with two forms on it. The second form's onLoad event calls initialize(). However, thisForm is (obviously) pointing to the first form on the document. I can't figure out how to make a generic reference to the current form, no matter where it is on the document (I can't use the form name, because that's different on each form).
How can I create a generic reference to the current form?
Thanks,
Jeff
Ok, if you call initialize from you form, try calling it like this:
<form onload="initialize(this);">The "this" tells the form to reference itself, and pass the value to the function initialize.
Now, in your initialize function, set it up like this:
function initialize(frm) {
thisForm = frm;
}
JSchwarz
06-10-2003, 08:59 AM
Thanks, Pyro. Good answer. Looks like I've got some mass changes to my onLoad events to make <G>.
Followup question: I tried investigating the 'this' object in both the onLoad() event and the initialize() function (using my favorite piece of stolen code, below) to try and determine what object it represents in both places. But I can't tell from the output and, more importantly, the 'name' property was blank (instead of the name I assigned the form). So how do we know its representing the current form if it doesn't have the correct name? How can we figure out exactly what it represents in all situations?
As you can tell, I'm trying to learn 'this' better. Thanks for your help.
{
var obj=this;
var names=new Array();
var strnames="";
var i=0;
for (prps in obj) {
names[i++] = prps + ':\t' + obj[prps];
} // for (prps in obj)
names.sort();
for (var i=0; i<names.length; ) {
strnames += names[i++] + "\n";
if (i % 20 == 0) {
alert(strnames);
strnames="";
}
}
alert(strnames);
}
Vladdy
06-10-2003, 09:19 AM
While <form onload... may work in some browsers, it is not a part of the current specification and therefore should be avoided.
Onload event is defined for document body and that is where it should be used.
If you your forms require the same initialization function you can iterate through your forms like this.
for(var i=0; i<document.forms.length; i++) formInitialize(document.forms[i]);
where your generic formInitialize function takes form element as an arguement and works with it:
funciton formInitialize(frm)
{ /* code here */
}
If you require different initialization routines for your forms you can create an array of initalization functions:
formsInit['firstForm'] = function (frm)
{ //first form initialization
};
formsInit['secondForm'] = function (frm)
{ //second form initialization
};
and given your forms respective ids:
<form id="firstForm" ...
<form id="secondForm" ...
loop through initialization functions like this
for (formID in formsInit) formsInit[formID](document.getElementById(formID));
JSchwarz
06-10-2003, 10:30 AM
I just found out that the first response is not working.
I still haven't figured out how to tell what type an object is. But this code is returning false for forms where the first one *is* the one I want to point to:
alert(document.forms[0] == thisForm);
Plus, all my code is broken using the first response (I posted too quickly that it was working).
Vladdy, I'm not trying to create different initialize routines for each form. I want to use the same initialize() for each form, and in initialize(), I want to create a variable, named thisForm, which points to the current form.
Right now, thisForm is set to document.forms[0], which works great for 99% of my forms. But now I have a page with two forms on it, and I need 'thisForm' to point to the second form.
Any help?
Jeff
Vladdy
06-10-2003, 10:35 AM
for(var i=0; i<document.forms.length; i++) formInitialize(document.forms[i]);
JSchwarz
06-10-2003, 11:47 AM
What does formInitialize do? I still need to know the number of the form on the page to access it like this, right?
Put another way, I'm looking for the equivalent of 'this', in JavaScript objects. I want 'this' to be equal to the current form, then I can assign it to a global variable for use anywhere.
Vladdy
06-10-2003, 01:22 PM
Read my initial reply...
JSchwarz
06-10-2003, 03:37 PM
I guess my problem is that I don't see how to use thisfor(var i=0; i<document.forms.length; i++) formInitialize(document.forms[i]); I don't initialize all of the forms at once, which is how this seems to work. I have each form runing initialize() when it's loaded in the browser. When would formInitialize run? There must be something basic I'm missing.