473,809 Members | 2,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to figure out if an input box exists without getting an error

I can display the value of the following input box with
alert(parent.My Frame.document. MyForm.MyInput. value);

But if the frame, the form or the input box doesn't exist I get of course an
error message.
How can I figure out before I display the value if the frame 'MyFrame', the
form 'MyForm' and also the input box 'MyInput' exists without getting an
error message?

Stefan
Nov 23 '05
28 2118
Thomas 'PointedEars' Lahn wrote in news:87******** *********@Point edEars.de
in comp.lang.javas cript:
setTimeout("myF oo()",1000);ret urn;


Seriously think about that for a minute! You set probably thousands of
timeouts here (since it runs in a tight loop) to go off 1000 milliseconds
(1 second) later.


You appear to have missed the return statment.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Nov 23 '05 #11


Stefan Mueller wrote:

But I not really understand why to do
if (parent.MyFrame && parent.MyFrame. document &&
parent.MyFrame. document.MyForm && parent.MyFrame. document.MyForm .MyInput) {


The && operator (|| as well but not used here) does some lazy evaluation
of operand expressions which allows you to write such step by step
checks without causing errors.
If you only tried to check
if (parent.MyFrame .document.MyFor m.MyInput)
then in various cases e.g. if
parent.MyFrame
is undefined or null or if
parent.MyFrame. document
is undefined or null or if
parent.MyFrame. document.MyForm
is undefined or null the expression will give an error (e.g.
... is null or not an object in IE
or
... has no properties in Mozilla
).

As
expression1 && expression2
evaluates expression1, converts to a boolean and if that is false simply
returns the result of expression1 for the whole "and expression" however
you can safely do the step by step checks and need to do them if you
want to handle all possible cases (e.g. no parent.MyFrame frame or no
document loaded in parent.MyFrame or no MyForm form in the document in
parent.MyFrame frame).

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 23 '05 #12
Rob Williscroft wrote:
Thomas 'PointedEars' Lahn wrote:
setTimeout("myF oo()",1000);ret urn;


Seriously think about that for a minute! You set probably
thousands of timeouts here (since it runs in a tight loop)
to go off 1000 milliseconds (1 second) later.


You appear to have missed the return statment.


Ah yes. I missed that too. One of the manifestations of DrClue's poor
coding is poor code formatting. The results are hard to read and
comprehend (with the inevitable consequences for code maintenance and
increased chance of erroneous or foolish coding). He wrote:-

| function myFoo()
| {
| for(;;) {
| if(parent.MyFra me)if(parent.My Frame.document)
| if(parent.MyFra me.document.get ElementById("My Input"))
| break;
|
| setTimeout("myF oo()",1000);ret urn;
| }// End forever
| alert(parent.My Frame.document. MyForm.MyInput. value);
| }

(and failed to use spaces to indent the code so that the formatting
would be preserved for all readers)

It is commonly recommended that if statements that only have a single
statement following them still use a block statement, with the single
statement within that block. This avoids confusion as to the logic of
the if statement and avoids mistakes when adding lines of code. If I was
formatting that function I would have formatted it as:-

function myFoo(){
for(;;){
if(parent.MyFra me){
if(parent.MyFra me.document){
if(parent.MyFra me.document.get ElementById("My Input")){
break;
}
}
}
setTimeout("myF oo()",1000);
return;
}
alert(parent.My Frame.document. MyForm.MyInput. value);
}

- as that better exposes the logic of the if statements and makes the
return more obvious. And in doing that it becomes self-evident that the
code was written by a fool. The - for - statement may loop forever,
unless broken, but it cannot loop more than once because it cannot
escape the return stamen it contains, unless it is broken. There is
simply no need to have a - for - statement here at all:-

function myFoo(){
if(
(parent.MyFrame )&&
(parent.MyFrame .document)&&
(parent.MyFrame .document.getEl ementById("MyIn put"))
){
alert(parent.My Frame.document. MyForm.MyInput. value);
}else{
setTimeout("myF oo()",1000);
}
}

- is a simpler and much more rational approach to the same outcome.

Richard.
Nov 23 '05 #13
Stefan Mueller wrote:
if (parent.frames['MyFrame'] && parent.frames['MyFrame'].document)


I'm doing my tests exactly like you told me and it works perfect.
But I not really understand why to do
if (parent.MyFrame && parent.MyFrame. document &&
parent.MyFrame. document.MyForm &&
parent.MyFrame. document.MyForm .MyInput) {

Why can't I just do
if (parent.MyFrame .document.MyFor m.MyInput) {
?

I know it doesn't work but I really don't understand that
because I think that in both cases if the MyInput text box
doesn't exist (parent.MyFrame .document.MyFor m.MyInput)
returns false.


The issue is that to resolve:-

parent.MyFrame. document.MyForm .MyInput

The language must first resolve:-

parent.MyFrame. document.MyForm

- and before that:-

parent.MyFrame. document

- and before that:-

parent.MyFrame

- and before that:-

parent

Or, more precisely, it resolves - parent - aginast the scope chain and
then atempts to get the value of a 'MyFrame' property of whatever -
parent - resolves as, and then attempts to get the value of a 'document'
property of that second value, then a 'MyForm' of the third value, and
then a 'MyInput' property of the fourth value.

If at any point one of these evaluates to the values of undefined or
null the subsequent attempt to read a property name from the value will
produce a run-time error and the test will never complete.

You may be fairly confident that - parent - refers to an object in a web
browser environment but 'MyFrame' will not be defined until the HTML
parser has seen the FRAME element (and not necessarily immediately upon
that happening (or at all)). MyFrame may not have a 'document' property
until it starts downloading to contents of the frame, its document will
not have a 'MyForm' property before the opening FORM tag is encountered
by the HTML parser, and 'MyInput will not be a property before the INPUT
element is encountered.

The browser object model is built up progressively so if the existence
of anything is an issue the existence of all the intermediate objects
would also be an issue.

Richard.
Nov 23 '05 #14
>It is commonly recommended that if statements that only have a single
statement following them still use a block statement, with the single
statement within that block. This avoids confusion as to the logic of
the if statement and avoids mistakes when adding lines of code.


Hah - that's funny.

I was told about a week or so ago in alt.www.webmaster that people who
did that were "unskilled" programmers, and shouldn't be programming.

Nov 23 '05 #15
Tony wrote:
It is commonly recommended that if statements that only have a single
statement following them still use a block statement, with the single
statement within that block. This avoids confusion as to the logic of
the if statement and avoids mistakes when adding lines of code.

Hah - that's funny.
I was told about a week or so ago in alt.www.webmaster that people who
did that were "unskilled" programmers, and shouldn't be programming.


It's a question of style, not a hard rule.

I agree than even if there is only one statement inside a condition, it
should be a block bounded by { }.

There is simply no reason _not_ to, but several good reasons in favor of it.

Those advising you in the other newsgroup are in error, IMO.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Nov 23 '05 #16
Matt Kruse wrote:
Tony wrote:
It is commonly recommended that if statements that only have a single
statement following them still use a block statement, with the single
statement within that block. This avoids confusion as to the logic of
the if statement and avoids mistakes when adding lines of code.


Hah - that's funny.
I was told about a week or so ago in alt.www.webmaster that people who
did that were "unskilled" programmers, and shouldn't be programming.

It's a question of style, not a hard rule.

I agree than even if there is only one statement inside a condition, it
should be a block bounded by { }.

There is simply no reason _not_ to, but several good reasons in favor of it.

Those advising you in the other newsgroup are in error, IMO.


Interesting to note that Douglas Crockford in his article /Private
Members in JavaScript/ includes this:
function dec() {
if (secret > 0) {
secret -= 1;
return true;
} else {
return false;
}
}
which could be reduced to:

function dec() {return secret && secret--};
For instruction purposes the longer version is much simpler to
understand, even though it may not be used in practice. The longer code
is about 15% slower in Firefox (about 800ms vs 920ms), with no
measurable difference in IE (about 2100ms) when measured over 50,000 loops.

The entire difference in speed results from the way secret is
decremented - 'secret -= 1' versus 'secret--'.
1. An immensely helpful article:
<URL:http://www.crockford.c om/javascript/private.html>
--
Rob
Nov 23 '05 #17
Tony said the following on 11/21/2005 8:09 PM:
It is commonly recommended that if statements that only have a single
statement following them still use a block statement, with the single
statement within that block. This avoids confusion as to the logic of
the if statement and avoids mistakes when adding lines of code.

Hah - that's funny.


Funny - maybe. True - without a doubt.
I was told about a week or so ago in alt.www.webmaster that people who
did that were "unskilled" programmers, and shouldn't be programming.


Sounds like a.w.w has a few "unskilled programmers" in the midst.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 23 '05 #18
RobG wrote:
function dec() {
if (secret > 0) {
secret -= 1;
return true;
} else {
return false;
}
}
which could be reduced to:
function dec() {return secret && secret--};
No it couldn't.
Your function returns the original value of 'secret' rather than a boolean.
In the cases where 'secret' is negative, the negative number evaluates to
true, rather than false, which is different behavior than the original.

Test case:

function dec1(secret) {
if (secret > 0) {
secret -= 1;
return true;
} else {
return false;
}
}
function dec2(secret) {return secret && secret--};
for (var i=-2; i<=2; i++) {
alert(i + "|" + dec1(i) + "|" + dec2(i) + "/" +
(dec2(i)?"true" :"false") );
}

A shorter function which does match the original functionality is:

function dec() { return (secret-- > 0); }
For instruction purposes the longer version is much simpler to
understand, even though it may not be used in practice. The longer
code is about 15% slower in Firefox (about 800ms vs 920ms), with no
measurable difference in IE (about 2100ms) when measured over 50,000
loops.


A 120ms difference over 50,000 loops is meaningless.
Over a single iteration, in a single browser, that means a .0000024 second
difference, which is insignificant.
Since the original is simpler to understand, I think it is the better choice
overall if the code is to be used or understood by a general audience. If
the code is meant to be dense and used by a limited audience, my revised
dec() might be a better choice.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Nov 23 '05 #19
RobG wrote:
function dec() {
if (secret > 0) {
secret -= 1;
return true;
} else {
return false;
}
}
which could be reduced to:
function dec() {return secret && secret--};
No it couldn't.
Your function returns the original value of 'secret' rather than a boolean.
In the cases where 'secret' is negative, the negative number evaluates to
true, rather than false, which is different behavior than the original.

Test case:

function dec1(secret) {
if (secret > 0) {
secret -= 1;
return true;
} else {
return false;
}
}
function dec2(secret) {return secret && secret--};
for (var i=-2; i<=2; i++) {
alert(i + "|" + dec1(i) + "|" + dec2(i) + "/" +
(dec2(i)?"true" :"false") );
}

A shorter function which does match the original functionality is:

function dec() { return (secret-- > 0); }
For instruction purposes the longer version is much simpler to
understand, even though it may not be used in practice. The longer
code is about 15% slower in Firefox (about 800ms vs 920ms), with no
measurable difference in IE (about 2100ms) when measured over 50,000
loops.


A 120ms difference over 50,000 loops is meaningless.
Over a single iteration, in a single browser, that means a .0000024 second
difference, which is insignificant.
Since the original is simpler to understand, I think it is the better choice
overall if the code is to be used or understood by a general audience. If
the code is meant to be dense and used by a limited audience, my revised
dec() might be a better choice.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com

Nov 23 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1870
by: Oeln | last post by:
If I want to check for input of an integer I've got the following (I get the form input with $input = "$_POST"): if(!ereg("^+$",$_POST)) { echo "Input is incomplete or incorrect."; } If, instead of only getting one 'input' I wanted to get n instances of input, I'd generate input fields for each of n instances I want in a for loop, then get the input with:
4
11125
by: lawrence | last post by:
Google can't find me a good example of how to use the "if exists" syntax in MySql. Is it right that to use it this way: INSERT INTO IF EXISTS tMyTable VALUES("xxlk", "lkjlkjlkjljk") I want to insert into a table but only if the table exists. How does one, in general, from PHP, test for the existence of a table, without getting an error message?
1
20469
by: Scott Shaw | last post by:
Hi all, I was wondering if you could help out with this problem that I am having. What I am trying to do is detect keyboard input in a while loop without halting/pausing the loop until the key is pressed (without hitting return). I looked at serveral faq's on the net and installed the cspan readkey module and neither seems to work most likey its me since I am getting frustrated. but anyway here's a sample code. while (1) { if...
3
13721
by: Gareth Tonnessen | last post by:
I need to have a clean way to determine whether a query exists without trying to open it and getting an error message. Is there a simple way to determine whether an object exists in a database? Remove obvious from e-mail address. Gareth Tonnessen LuvTruth@att.netNOJUNK
2
1623
by: tshad | last post by:
I am getting an error on a object name that doesn't exist (according to asp.net), but if you look at the trace, it does. Here is the error: ******************************************************************************************* Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler...
3
6654
by: Ryan Ternier | last post by:
Customers keep getting this error, but we've never been able to re-produce it. Any help on this would be awesome. Here's the error message we get. Date and Time Occured: Wednesday, June 01, 2005 11:02:20 AM
15
4784
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update button will verify the information that has been entered and updates the data base if the data is correct. Update will throw an exception if the data is not validate based on some given rules. I also have a custom error handling page to show the...
2
1726
by: kkleung89 | last post by:
Basically, here's what's happening with the program. I have a table of Customers and a table of Pets, with the latter containing a field linking it to its customer of ownership. I have a form with all of the personal information of the customers, and within it is a list box with all of the pets in it. In the current design, it takes that information from a query, but since every customer is different, the query needs to change each...
8
2689
lifeisgreat20009
by: lifeisgreat20009 | last post by:
What might be the possible cause ? I have created a website using struts framework, jsp In my transaction page the money is not getting transferred.. When I hit the submit button in my Transaction.jsp page , no transaction happens .. Only the url changes from http://localhost:8080/bankfinalproject/jsp/transaction.jsp to this
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10633
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10375
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.