With clause syntax errors but how do I fix it?

P

Phil Powell

<script>
<!--

function isValidAlert() {
for (var i = 0; i < document.alertForm.length; i++) {
with (document.alertForm.elements) {
if (.name == "text" || .name == "password" || .name == "textarea") {
if (.value == "") {
alert("Please enter a " + .name);
focus();
return false;
}
} else if (.name == "select-one") {
if (.options[document.alertForm.elements.selectedIndex].value ==
"") {
alert("Please select a " + .name);
return false;
}
}
}
}
}

//-->
</script>

I don't know the answer to this problem, I am getting syntax errors, and
"with" does not search well on any Javascript tutorial. So this is my only
option for help; can someone tell me what I did wrong?

Thanx
Phil
 
L

Laurent Bugnion, GalaSoft

Hi,

Phil said:
<script>
<!--

function isValidAlert() {
for (var i = 0; i < document.alertForm.length; i++) {
with (document.alertForm.elements) {
if (.name == "text" || .name == "password" || .name == "textarea") {
if (.value == "") {
alert("Please enter a " + .name);
focus();
return false;
}
} else if (.name == "select-one") {
if (.options[document.alertForm.elements.selectedIndex].value ==
"") {
alert("Please select a " + .name);
return false;
}
}
}
}
}

//-->
</script>

I don't know the answer to this problem, I am getting syntax errors, and
"with" does not search well on any Javascript tutorial. So this is my only
option for help; can someone tell me what I did wrong?

Thanx
Phil


"with" works in VB, but as far as I know, it doesn't in JavaScript. It
also doesn't in most current languages. Anyway, it makes the code
unreadable, and all in all it is good practice to not use it.

If you want to simplify long or complicated structures, you can always
redeclare them:

var myText = windowNumberOne.document.theNameOfTheForm.theNameOfTheText;

myText.value = "Hello world";

Laurent
 
L

Laurent Bugnion, GalaSoft

Correction, I was wrong. See below.

Hi,

Phil said:
<script>
<!--

function isValidAlert() {
for (var i = 0; i < document.alertForm.length; i++) {
with (document.alertForm.elements) {
if (.name == "text" || .name == "password" || .name == "textarea") {
if (.value == "") {
alert("Please enter a " + .name);
focus();
return false;
}
} else if (.name == "select-one") {
if (.options[document.alertForm.elements.selectedIndex].value ==
"") {
alert("Please select a " + .name);
return false;
}
}
}
}
}

//-->
</script>

I don't know the answer to this problem, I am getting syntax errors, and
"with" does not search well on any Javascript tutorial. So this is my
only
option for help; can someone tell me what I did wrong?

Thanx
Phil



"with" works in VB, but as far as I know, it doesn't in JavaScript. It
also doesn't in most current languages. Anyway, it makes the code
unreadable, and all in all it is good practice to not use it.

If you want to simplify long or complicated structures, you can always
redeclare them:

var myText = windowNumberOne.document.theNameOfTheForm.theNameOfTheText;

myText.value = "Hello world";

Laurent


OK, I was wrong. With exists in JavaScript, as described in this example:

It is often convenient to use the with statement when a section of code
uses several math constants and methods, so you don't have to type
"Math" repeatedly. For example,

with (Math) {
a = PI * r*r
y = r*sin(theta)
x = r*cos(theta)
}

So the syntax is different (you don't use the '.' like in VB).

I stand, however, by the fact that it's bad practice to use it, and I
really recommend that you don't.

HTH,

Laurent
 
P

Phil Powell

Wow, damned one way or another.. that is awful!

I don't know what is more unreadable, how you view a "with" clause, or this:

document.alertForm.elements.options[document.alertForm.elements.select
edIndex].value

I think that is utterly unreadable myself! I was trying to come up with an
elegant and readable solution to parse through form elements for client-side
validation. :(

I remember you use "." in VB. Sorry but it's now

VB 1
Javascript 0

That is very unsafe to not use "." because of potential data collision.

Phil

Laurent Bugnion said:
Correction, I was wrong. See below.

Hi,

Phil said:
<script>
<!--

function isValidAlert() {
for (var i = 0; i < document.alertForm.length; i++) {
with (document.alertForm.elements) {
if (.name == "text" || .name == "password" || .name == "textarea") {
if (.value == "") {
alert("Please enter a " + .name);
focus();
return false;
}
} else if (.name == "select-one") {
if (.options[document.alertForm.elements.selectedIndex].value ==
"") {
alert("Please select a " + .name);
return false;
}
}
}
}
}

//-->
</script>

I don't know the answer to this problem, I am getting syntax errors, and
"with" does not search well on any Javascript tutorial. So this is my
only
option for help; can someone tell me what I did wrong?

Thanx
Phil



"with" works in VB, but as far as I know, it doesn't in JavaScript. It
also doesn't in most current languages. Anyway, it makes the code
unreadable, and all in all it is good practice to not use it.

If you want to simplify long or complicated structures, you can always
redeclare them:

var myText = windowNumberOne.document.theNameOfTheForm.theNameOfTheText;

myText.value = "Hello world";

Laurent


OK, I was wrong. With exists in JavaScript, as described in this example:

It is often convenient to use the with statement when a section of code
uses several math constants and methods, so you don't have to type
"Math" repeatedly. For example,

with (Math) {
a = PI * r*r
y = r*sin(theta)
x = r*cos(theta)
}

So the syntax is different (you don't use the '.' like in VB).

I stand, however, by the fact that it's bad practice to use it, and I
really recommend that you don't.

HTH,

Laurent
--
Laurent Bugnion, GalaSoft
Webdesign, Java, JavaScript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
 
D

DU

Phil said:
<script>
<!--

function isValidAlert() {
for (var i = 0; i < document.alertForm.length; i++) {
with (document.alertForm.elements) {
if (.name == "text" || .name == "password" || .name == "textarea") {


This seems suspicious. I bet it would be rather:

if (type == "text" || type == "password" || type == "textarea") {
if (.value == "")

if(value == "")

{
alert("Please enter a " + .name);
focus();
return false;
}
} else if (.name == "select-one") {
if (.options[document.alertForm.elements.selectedIndex].value ==
"") {
alert("Please select a " + .name);
return false;


Remove "." everywhere and try to make your code more compact.
}
}
}
}
}

//-->
</script>

I don't know the answer to this problem, I am getting syntax errors, and
"with" does not search well on any Javascript tutorial. So this is my only
option for help; can someone tell me what I did wrong?

Thanx
Phil

It is widely known that with statements are very cpu-demanding, RAM
demanding also and are best to be avoided. Even MSDN recommends this.

DU
--
Javascript and Browser bugs:
http://www10.brinkster.com/doctorunclear/
- Resources, help and tips for Netscape 7.x users and Composer
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
http://www10.brinkster.com/doctorunclear/Netscape7/Netscape7Section.html
 
D

DU

Phil said:
Wow, damned one way or another.. that is awful!

I don't know what is more unreadable, how you view a "with" clause, or this:

document.alertForm.elements.options[document.alertForm.elements.select
edIndex].value

I think that is utterly unreadable myself!


Then just use a local variable to store each element (at the start of
your for loop). E.g.:

var IteratedFormElement = document.alertForm.elements;
(...)

if(IteratedFormElement.options[IteratedFormElement.selectedIndex].value
== "")
(...)

I was trying to come up with an
elegant and readable solution to parse through form elements for client-side
validation. :(

[snipped]

Finally, please avoid top-posting in this newsgroup. Top-posting
destroys logical reading context and chronological order of posts in
threads.

DU
--
Javascript and Browser bugs:
http://www10.brinkster.com/doctorunclear/
- Resources, help and tips for Netscape 7.x users and Composer
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
http://www10.brinkster.com/doctorunclear/Netscape7/Netscape7Section.html
 
P

Phil Powell

top-posting? HUH? English, please, or Svenska.

Phil

DU said:
Phil said:
Wow, damned one way or another.. that is awful!

I don't know what is more unreadable, how you view a "with" clause, or this:

document.alertForm.elements.options[document.alertForm.elements.select
edIndex].value

I think that is utterly unreadable myself!


Then just use a local variable to store each element (at the start of
your for loop). E.g.:

var IteratedFormElement = document.alertForm.elements;
(...)

if(IteratedFormElement.options[IteratedFormElement.selectedIndex].value
== "")
(...)

I was trying to come up with an
elegant and readable solution to parse through form elements for client-side
validation. :(

[snipped]

Finally, please avoid top-posting in this newsgroup. Top-posting
destroys logical reading context and chronological order of posts in
threads.

DU
--
Javascript and Browser bugs:
http://www10.brinkster.com/doctorunclear/
- Resources, help and tips for Netscape 7.x users and Composer
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
http://www10.brinkster.com/doctorunclear/Netscape7/Netscape7Section.html
 
J

Janwillem Borleffs

Top-posting is putting your reply on top of the message or phrase you are
replying to. As I'm doing now...

Phil Powell said:
top-posting? HUH? English, please, or Svenska.

-----------------

Phil Powell said:
top-posting? HUH? English, please, or Svenska.

This is the proper way of replying to a message by putting your reply
beneath the message.


JW
 
J

Janwillem Borleffs

Phil Powell said:
I remember you use "." in VB. Sorry but it's now

VB 1
Javascript 0

That is very unsafe to not use "." because of potential data collision.

No, it's not, because the scope is limited to the with clause itself. Try
the following example:

<html>
<head>
<title> New Document </title>
<script type="text/javascript">
name = "Hello 1";

function doAlert() {
var name = "Hello 2";
with (document.forms[0].elements[0]) {
alert("Form element's name value: " + name);
}
alert("Global var name value: " + window['name']);
alert("Local var name value: " + name);
}
</script>
</head>

<body>
<form>
<input type="text" value="text" name="test">
<input type="button" onClick="doAlert()" value="Test">
</form>
</body>
</html>

BTW, I don't subscribe to Laurent's point of view regarding the with clause.


JW
 
P

Phil Powell

Ok, assuming that you have the with clause with your example. See my
comments below.

Phil

Janwillem Borleffs said:
Phil Powell said:
I remember you use "." in VB. Sorry but it's now

VB 1
Javascript 0

That is very unsafe to not use "." because of potential data collision.

No, it's not, because the scope is limited to the with clause itself. Try
the following example:

<html>
<head>
<title> New Document </title>
<script type="text/javascript">
name = "Hello 1";

function doAlert() {
var name = "Hello 2";
with (document.forms[0].elements[0]) {
alert("Form element's name value: " + name);
}
alert("Global var name value: " + window['name']);
alert("Local var name value: " + name);
}
</script>

Ok, now, what if you need both the var "name" AND
document.forms[0].elements[0].name at the same time? You see, with
Javascript not permitting the VB solution you can't do both so easily. VB
you would have "name" and ".name".

Phil
 
P

Phil Powell

Maybe it's a comp.lang.javascript thing but I don't get it. What's wrong
with that? That is the conventional reply. I'd never think to look below
unless you direct me to look below for comments on a particular area; and I
only do that when I need to highlight something you have said.

I think DU's logic flow and mine are in different worlds.

Phil
 
J

Janwillem Borleffs

Phil Powell said:
Ok, now, what if you need both the var "name" AND
document.forms[0].elements[0].name at the same time? You see, with
Javascript not permitting the VB solution you can't do both so easily. VB
you would have "name" and ".name".

Yes it does, but you must define the variable as a property of the function:

function doAlert() {
with (document.forms[0].elements[0]) {
alert("Form element's name value: " + name);
alert("Local var name value: " + doAlert.name);
}
}

doAlert.name = "Hello There";

I admit, with VB the syntax is more convenient. But as you can see, there
are ways of getting there with JavaScript too.

In a way, it even forces you to think about your program design. E.g., when
my script encounters a pre-defined property 'name', then I shouldn't define
a variable with the same name. This is a good thing in my point of view.


JW
 
P

Phil Powell

Never mind, you lost me completely.

Phil

DU said:
http://www10.brinkster.com/doctorunclear/Netscape7/Netscape7Section.html
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
- Resources, help and tips for Netscape 7.x users and Composer
http://www10.brinkster.com/doctorunclear/
Javascript and Browser bugs:
--
DU


top to bottom, not from bottom to top.
preferences then? Because 99.99% of people in this newsgroup read from
Are you saying that you read from bottom to top? What are your reading
Phil

I think DU's logic flow and mine are in different worlds.

only do that when I need to highlight something you have said.
unless you direct me to look below for comments on a particular area; and I
with that? That is the conventional reply. I'd never think to look below
Maybe it's a comp.lang.javascript thing but I don't get it. What's
wrong
Phil Powell wrote:

[snipped]
 
P

Phil Powell

That is a Java/CF/PHP solution moreso than Javascript. It appears
Javascript is becoming more like Java every day!!!

I never knew you could do that!

Phil

Janwillem Borleffs said:
Phil Powell said:
Ok, now, what if you need both the var "name" AND
document.forms[0].elements[0].name at the same time? You see, with
Javascript not permitting the VB solution you can't do both so easily. VB
you would have "name" and ".name".

Yes it does, but you must define the variable as a property of the function:

function doAlert() {
with (document.forms[0].elements[0]) {
alert("Form element's name value: " + name);
alert("Local var name value: " + doAlert.name);
}
}

doAlert.name = "Hello There";

I admit, with VB the syntax is more convenient. But as you can see, there
are ways of getting there with JavaScript too.

In a way, it even forces you to think about your program design. E.g., when
my script encounters a pre-defined property 'name', then I shouldn't define
a variable with the same name. This is a good thing in my point of view.


JW
 
J

Jim Ley

That is a Java/CF/PHP solution moreso than Javascript. It appears
Javascript is becoming more like Java every day!!!

Javascript hasn't changed in a number of years....
function doAlert() {
with (document.forms[0].elements[0]) {
alert("Form element's name value: " + name);
alert("Local var name value: " + doAlert.name);
}
}

with is considerably slower to execute and best avoided entirely.

Please learn how to post, it's not "top or bottom", it's snipped
interleaved quoting, your lack of snipping is your biggest problem.

Jim.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
Finally, please avoid top-posting in this newsgroup.

The term "top-posting" should not be used to a newcomer, since it will
not be understood by the average innocent top-poster.

Something like "responses should follow trimmed quotes" does not take
much longer to type.

Alternatively, if "top-posting" is used, the term should be defined in
the FAQ, and "; see FAQ" added. Plus, preferably, a link to the FAQ.



IMHO, javascript 'with' is a useful tool which can readily be misused.
IMHO, it can be good to use it when anyone reading the code for the
first time will, having not noticed the 'with', be able to deduce it.

with (document) {
write('This<br>')
write(That, [them])
}

with (MyDate)
document.write(getFullYear()+'/'+LZ(getMonth()+1)+
'/'+LZ(getDate()))

Where the contents of the with parentheses would be lengthy, the
alternative of using an auxiliary variable is that a new variable,
probably terse, will remain in scope until the end of the block; a human
author might reuse it without remembering to give it a new value.
 
D

Douglas Crockford

I don't know what is more unreadable, how you view a "with" clause, or this:
document.alertForm.elements.options[document.alertForm.elements.select
edIndex].value

I think that is utterly unreadable myself! I was trying to come up with an
elegant and readable solution to parse through form elements for client-side
validation. :(


The with statement should be avoided. Use a var to do what you would do with
with.

var e = document.alertForm.elements;
... e.options[e.selectedIndex].value ...

The var lets you be explicit, while the with can have surprising and seemingly
random behavior. The appearance of with in the language was well intentioned,
but it was a mistake and it should be removed.

http://www.crockford.com/javascript/survey.html
 
D

DU

Dr said:
JRS: In article <[email protected]>, seen in



The term "top-posting" should not be used to a newcomer, since it will
not be understood by the average innocent top-poster.

How do you figure out who is a newcomer in a newsgroup? How can you
reasonably do that?
Something like "responses should follow trimmed quotes" does not take
much longer to type.

Alternatively, if "top-posting" is used, the term should be defined in
the FAQ, and "; see FAQ" added. Plus, preferably, a link to the FAQ.

I'm all for defining "top-posting" in this newsgroup FAQ and I'm all for
explaining why top-posting just makes reading posts, replies and threads
harder for others.
Top-posting is not the natural reading direction for 99.99% of users in
this newsgroup... unless you really read from bottom to top (like
ancient Chinese).
Top-posting destroys chronological order of posts in threads.
Top-posting destroys logical reading context.
Top-posting makes others constantly scroll up and down to figure out
what was said and then what was replied: there is no book, document,
paper anywhere which goes like that. No one reads a book from end to
start. No one reads an interview in a magazine by first reading the
answers to questions. No one reads solutions first and then problem
descriptions. I don't video-tape a movie from end to start.
Top-posting generally makes it harder to understand message posts context.

DU
--
Javascript and Browser bugs:
http://www10.brinkster.com/doctorunclear/
- Resources, help and tips for Netscape 7.x users and Composer
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
http://www10.brinkster.com/doctorunclear/Netscape7/Netscape7Section.html
 
P

Phil Powell

Thanx for your help.

DU said:
How do you figure out who is a newcomer in a newsgroup? How can you
reasonably do that?


I'm all for defining "top-posting" in this newsgroup FAQ and I'm all for
explaining why top-posting just makes reading posts, replies and threads
harder for others.
Top-posting is not the natural reading direction for 99.99% of users in
this newsgroup... unless you really read from bottom to top (like
ancient Chinese).
Top-posting destroys chronological order of posts in threads.
Top-posting destroys logical reading context.
Top-posting makes others constantly scroll up and down to figure out
what was said and then what was replied: there is no book, document,
paper anywhere which goes like that. No one reads a book from end to
start. No one reads an interview in a magazine by first reading the
answers to questions. No one reads solutions first and then problem
descriptions. I don't video-tape a movie from end to start.
Top-posting generally makes it harder to understand message posts context.

DU
--
Javascript and Browser bugs:
http://www10.brinkster.com/doctorunclear/
- Resources, help and tips for Netscape 7.x users and Composer
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
http://www10.brinkster.com/doctorunclear/Netscape7/Netscape7Section.html
 
L

Lasse Reichstein Nielsen

Douglas Crockford said:
The var lets you be explicit, while the with can have surprising and
seemingly random behavior.

Can you give examples of deliberate use of "with" that has surpricing
and seemingly random behavior?
The appearance of with in the language
was well intentioned, but it was a mistake and it should be removed.

(where all you say is "The with statement should be avoided" :))
/L
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top