yet another 'object has no properties' error

D

droesler

I have a function that receives a form name and field name.

someFunc(formName, fieldName) {
var fieldVal = document.forms[formName].elements[fieldName].value;
[snip]
}

This triggers the 'has no properties error. An alert message shows the
correct values are being passed in. If I hardcode
var fieldVal = document.realFormName.realFieldName.value;
no error is generated and fieldVal has the value of the input field.

An alert(document.forms.length) displays 1, which is what I would
expect. It seems if I try to access some part of the document through
variables in javascript errors are generated. If I hard code document
object names in the javascript then the javascript functions as
expected.

The generated document passes the w3c validator test, although that
doesn't look at all of the sourced javascript files; and the Mozilla
JavaScript console shows no errors until the above function is called.

How can I troubleshoot this problem?

Thanks

Dennis
 
M

Martin Honnen

I have a function that receives a form name and field name.

someFunc(formName, fieldName) {
var fieldVal = document.forms[formName].elements[fieldName].value;
[snip]
}

This triggers the 'has no properties error.
The generated document passes the w3c validator test, although that
doesn't look at all of the sourced javascript files; and the Mozilla
JavaScript console shows no errors until the above function is called.

Show us the function call so that we know the actual parameter values,
then show us the corresponding HTML markup for the form and the element.
Or post a URL where the problem occurs. Does the problem only occur
with Mozilla or other browsers too?
 
D

droesler

Martin said:
I have a function that receives a form name and field name.

someFunc(formName, fieldName) {
var fieldVal = document.forms[formName].elements[fieldName].value;
[snip]
}

This triggers the 'has no properties error.
The generated document passes the w3c validator test, although that
doesn't look at all of the sourced javascript files; and the Mozilla
JavaScript console shows no errors until the above function is called.

Show us the function call so that we know the actual parameter values,
then show us the corresponding HTML markup for the form and the element.
Or post a URL where the problem occurs. Does the problem only occur
with Mozilla or other browsers too?

The HTML markup with the function call:
<a href="javascript:getEmail('officeInfo', 'officeEmail')"
title="Click here to select email addresses...">eMail</a>
<input type="text" name="officeEmail" value="$officeEmail"
tabindex="9">

The javascript function:
function getEmail(formName, fieldName) {
var email =
window.document.forms[formName].elements[fieldName].value;
if (email.length > 0) {
email += ",";
}
var link = "/cgi-bin/ldapEmailPickList.pl?formName=" + formName +
"&fieldName=" + fieldName;
link += "&emailList=" + email;
window.open(link, "", "menubar, scrollbars, resizable, width=650,
height=600");
return;
}
 
D

droesler

Martin said:
I have a function that receives a form name and field name.

someFunc(formName, fieldName) {
var fieldVal = document.forms[formName].elements[fieldName].value;
[snip]
}

This triggers the 'has no properties error.
The generated document passes the w3c validator test, although that
doesn't look at all of the sourced javascript files; and the Mozilla
JavaScript console shows no errors until the above function is called.

Show us the function call so that we know the actual parameter values,
then show us the corresponding HTML markup for the form and the element.
Or post a URL where the problem occurs. Does the problem only occur
with Mozilla or other browsers too?

The HTML markup with the function call:
<a href="javascript:getEmail('officeInfo', 'officeEmail')"
title="Click here to select email addresses...">eMail</a>
<input type="text" name="officeEmail" value="$officeEmail"
tabindex="9">

The javascript function:
function getEmail(formName, fieldName) {
var email =
window.document.forms[formName].elements[fieldName].value;
if (email.length > 0) {
email += ",";
}
var link = "/cgi-bin/ldapEmailPickList.pl?formName=" + formName +
"&fieldName=" + fieldName;
link += "&emailList=" + email;
window.open(link, "", "menubar, scrollbars, resizable, width=650,
height=600");
return;
}

Meant to add that it occurs in both Mozilla and IE.
 
L

Lee

(e-mail address removed) said:
Martin said:
(e-mail address removed) wrote:

I have a function that receives a form name and field name.

someFunc(formName, fieldName) {
var fieldVal = document.forms[formName].elements[fieldName].value;
[snip]
}

This triggers the 'has no properties error.

The generated document passes the w3c validator test, although that
doesn't look at all of the sourced javascript files; and the Mozilla
JavaScript console shows no errors until the above function is called.

Show us the function call so that we know the actual parameter values,
then show us the corresponding HTML markup for the form and the element.
Or post a URL where the problem occurs. Does the problem only occur
with Mozilla or other browsers too?

The HTML markup with the function call:
<a href="javascript:getEmail('officeInfo', 'officeEmail')"
title="Click here to select email addresses...">eMail</a>
<input type="text" name="officeEmail" value="$officeEmail"
tabindex="9">

The javascript function:
function getEmail(formName, fieldName) {
var email =
window.document.forms[formName].elements[fieldName].value;
if (email.length > 0) {
email += ",";
}
var link = "/cgi-bin/ldapEmailPickList.pl?formName=" + formName +
"&fieldName=" + fieldName;
link += "&emailList=" + email;
window.open(link, "", "menubar, scrollbars, resizable, width=650,
height=600");
return;
}

Meant to add that it occurs in both Mozilla and IE.

I'm sure you also meant to show the HTML markup for the form and element,
as requested.
But, in the meantime, don't abuse the javascript: pseudo-protocol.
If you're not really linking to something, don't use a link, or, if
you must, invoke your function from the onclick handler, not the HREF.

<a href="noscript.html"
title="Click here to select email addresses..."
onclick="getEmail('officeInfo', 'officeEmail');return false"
eMail</a>

where "noscript.html" is a page in which you apologize for having made
the design decision to not allow this feature to work unless scripting
is enabled. The "return false" as the last statement of the onclick
event handler ensures that they won't see that page if the script works.


--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top