Solved

Checking to see if a field exists in Javascript

Posted on 2000-03-15
8
250 Views
Last Modified: 2013-12-18
In Javascript, is there a way to check if a field exists before referring to it?
 
The reason I ask is whenever my document loads I execute the following passthrough Javascript.

<Script Language="Javascript">
{if (document.forms[0].customerName.value =="Error") {alert("The customer number is invalid");document.forms[0].customerNumber.focus()}}
</Script>

This works great as long as the document is in edit mode, however, if I try to open the document in read mode I get the error "document.forms[0].customerNumber is not an object".  Because the document is in read mode it doesn't see the customerNumber field.  How can I skip this code if the document is opened in read mode?  I can't use hide-when's because if I do the HTML style is taken away from my passthrough HTML.
0
Comment
Question by:snobunny
  • 3
  • 3
  • 2
8 Comments
 
LVL 6

Expert Comment

by:ghassan99
Comment Utility
What version of Notes r u using?
You can use hide/when without losing the passthrough HTML property, and your script will generate no errors.

-Gus
0
 
LVL 24

Expert Comment

by:HemanthaKumar
Comment Utility
Hi

You can test whether an object is available or not by testing for undefined value, here is the sample of it:
[<Body onLoad="test()">]

<SCRIPT Language="Javascript">
function test(){
      if (document.forms[0].customerName==undefined)
      alert("True");
}
</SCRIPT>




Good Luck
~Hemanth
0
 

Author Comment

by:snobunny
Comment Utility
Your example works in Netscape 4.08 (Actually my code always worked in Netscape) but still does not work in IE 4.0

I now get the following error message:  

Error: 'undefined' is undefined
0
 
LVL 6

Accepted Solution

by:
ghassan99 earned 200 total points
Comment Utility
I dont think its actually working in Netscape!  But probably netscape is more tolerant.  The proper way to test for a field is using the typeof operator, without that u r actually checking if the field has value or not.
Use it like this:

<Script Language="Javascript">
if (typeof document.forms[0].customerName != "undefined") {    
{if (document.forms[0].customerName.value =="Error") {alert("The customer number is invalid");document.forms[0].customerNumber.focus()}}}
</Script>

And I still think that hiding the text will not remove its passthru property.

-Gus
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 24

Expert Comment

by:HemanthaKumar
Comment Utility
Hi All

I have tested my code on NS and found to work, but I curse myself for not testing on the IE, anyway as gus mentioned typeof is preferable and works perfectly on both browsers.

~Hemanth
0
 

Author Comment

by:snobunny
Comment Utility
Your solution worked in NS but did not work on IE.  The solution that Gus provided worked on both.  I will award the points to Gus.  Thanks for answering my question.
0
 

Author Comment

by:snobunny
Comment Utility
Thanks for your answer, Gus.  I have tried it out and it works great!
0
 
LVL 6

Expert Comment

by:ghassan99
Comment Utility
No problem...:)

-Gus
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

For users on the Lotus Notes 8 Standard client, this article provides information on checking the Java Heap size and adjusting it to half of your system RAM in attempt to get the Lotus Notes 8.x Standard client to run faster.  I've had to exercise t…
You’ve got a lotus Domino web server, and you have been told that “leverage browser caching” is a must do. This means that we have to tell the browser everywhere in the web to use cache. In other words, we set (and send) an expiration date in the HT…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now