Solved

Checking to see if a field exists in Javascript

Posted on 2000-03-15
8
255 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
ID: 2623056
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
ID: 2623868
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
ID: 2625183
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 6

Accepted Solution

by:
ghassan99 earned 200 total points
ID: 2625881
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
 
LVL 24

Expert Comment

by:HemanthaKumar
ID: 2626569
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
ID: 2628751
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
ID: 2628756
Thanks for your answer, Gus.  I have tried it out and it works great!
0
 
LVL 6

Expert Comment

by:ghassan99
ID: 2630172
No problem...:)

-Gus
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Domino Mail Routing - Notes Name Networks, DNS, or connection documents? 5 141
Only send email if contains "abcd" 3 148
Domino Server 11 72
Code for extraction to mkdir folder 5 73
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…
For beginners of Lotus Notes user this is important to know about the types of files and their location supported by IBM Notes. Mostly users are unaware about how many file types are created and what their usages are. This Article is fully dedicated…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 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