Hmm. Anyone go chapter and verse on this one?

  • Thread starter The Natural Philosopher
  • Start date
T

The Natural Philosopher

Consider:

<script type="text/javascript" language="JavaScript">
function foo()
{
var dung;
dung=document.getElementsByName('foo')[0].value;
Alert(dung);
}
</script>
<INPUT type="hidden" name="foo" value="shite">


<b onclick="foo()">Click here for an opinion on Javascript</b>

Works in everything except IE. IE6/7 seems to regard
document.getElementsByName('foo') as including the function itself and
gives an 'error in page' response. Or perhaps it finds it first, and
that's why the error occurs.

I am sure there s some reason why it works in other browsers but not in
IE, but what is it?
 
T

The Natural Philosopher

Randy said:
The Natural Philosopher said the following on 10/17/2007 3:02 PM:
Consider:

<script type="text/javascript" language="JavaScript">
function foo()
{
var dung;
dung=document.getElementsByName('foo')[0].value;
Alert(dung);
}
</script>
<INPUT type="hidden" name="foo" value="shite">


<b onclick="foo()">Click here for an opinion on Javascript</b>

Works in everything except IE.

Firefox2.0: Alert is not defined.
Opera9: Statement on line 6: Reference to undefined variable: Alert

So, I don't believe your "Works in everything except IE".
IE6/7 seems to regard document.getElementsByName('foo') as including
the function itself and gives an 'error in page' response. Or perhaps
it finds it first, and that's why the error occurs.

No, it gives the error message (IE7) of "Object expected" because it is
trying to call a function named Alert with a parameter of dung and since
Alert doesn't exist it throws the error.

Try the Error Console in Firefox or Opera and reading the error messages
there.
I am sure there s some reason why it works in other browsers but not
in IE, but what is it?

I don't believe it works in other browsers because testing shows me that
it doesn't work in other browsers. Change it to alert instead of Alert
and try again.

That was the one bit I didn't test.;-)

you have focussed on an obvious bug and completely failed to address the
actual question.

Does that make you look smart? or stoopid?
 
T

The Natural Philosopher

Randy said:
The Natural Philosopher said the following on 10/17/2007 8:33 PM:



The "bug" being your total inability to write a script?

I wrote it and it worked, or didnt work in IE.

What I posted was a demonstrative snippet that I got wrong and didn't test.


The point being that if you have a function and a variable with the same
name, and do a getElementsbyName on the form, in firefox and opera the
input is the zero'th element of the returned array, in IE the browser
throws an error if you access the zeroth element.

Go troll somewhere else please.

Stoopid it is.
I guess you simply didn't know the answer.
 
T

The Natural Philosopher

Randy said:
The Natural Philosopher said the following on 10/18/2007 5:40 AM:
I wrote it and it worked, or didnt work in IE.

What I posted was a demonstrative snippet that I got wrong and didn't
test.
The point being that if you have a function and a variable with the
same name, and do a getElementsbyName on the form, in firefox and
opera the input is the zero'th element of the returned array, in IE
the browser throws an error if you access the zeroth element.

And I can't duplicate that. This code:

<script type="text/javascript">
function foo()
{
var dung;
dung=document.getElementsByName('foo')[0].value;
alert(dung);
}

</script>

<input type="hidden" name="foo" value="shite">
<button onclick="foo()">Test</button>

When I click that button in IE7, I get - "shite". No error, no problems,
it gives me what I expected. The only things I changed from your
original code was changing Alert to alert. So, what version of IE are
you getting that error in? And, what is the error message?

Ie6 and 7. 'error on page'. In the status bar at the bottom.


When I changed the variable names to make the functions NOT have the
same name as the variables, it worked.
 
G

GArlington

Randy said:
The Natural Philosopher said the following on 10/18/2007 5:40 AM:
And I can't duplicate that. This code:
<script type="text/javascript">
function foo()
{
var dung;
dung=document.getElementsByName('foo')[0].value;
alert(dung);
}
</script>

<input type="hidden" name="foo" value="shite">
<button onclick="foo()">Test</button>
When I click that button in IE7, I get - "shite". No error, no problems,
it gives me what I expected. The only things I changed from your
original code was changing Alert to alert. So, what version of IE are
you getting that error in? And, what is the error message?

Ie6 and 7. 'error on page'. In the status bar at the bottom.

When I changed the variable names to make the functions NOT have the
same name as the variables, it worked.

I can see that you are talking about the same thing not understanding
each other...
OP puts his/her Javasript INSIDE the body (NOT in head) before the
input field. So when he/she runs the script
document.getElementsByName('foo')[0] refers to the FUNCTION foo and
generates the error trying to access attribute value which function
WILL NOT have...
Everybody else assumes that Javascript is included in head of the page
and so do NOT experience any errors.
 
B

beegee

I am sure there s some reason why it works in other browsers but not in
IE, but what is it?

There is a bug in the implementation of getElementsByName in IE 6 and
7. The symptoms differ but your's are most like related. Read about
it and a fix, here:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1843487&SiteID=1&pageid=0

And please stop whining about javascript. Go to Yahoo User Interface
pages and watch Crockford's video on Advanced Javascript. Right now
you are looking at about 10% of the language and calling it crap. It
makes you look ignorant and trollish.

Bob
 
H

Henry

GArlington said the following on 10/18/2007 9:51 AM:
So when he/she runs the script document.getElementsByName('foo')[0]
refers to the FUNCTION foo and generates the error trying to access
attribute value which function WILL NOT have...

No, it doesn't. The function is not executed until you click on the
bold text in the original, the button in the latter. And, before
it is dreamed about, changing it back to a bold element didn't
make any difference either.

The code will not error on IE until someone puts the form elements
into a FORM element. At that point IE augments the scope chain of the
function that it creates from the intrinsic event attribute, and it
places the FORM element on that augmented scope chain. Thus when the
Identifier - foo - is resolved against this augmented scope chain an
object on the chain above the global object is found to have a 'foo'
property and the resulting value of - foo - is the value of that
property of the FORM element and not the global function.
Voodoo programming. Putting my script block before the closing body
tag had no impact on errors. I still don't get one.

It certainly is an odd proposition that moving the code into the HEAD
would make any difference.
 
T

The Natural Philosopher

GArlington said:
Randy said:
The Natural Philosopher said the following on 10/18/2007 5:40 AM:
Randy Webb wrote:
The Natural Philosopher said the following on 10/17/2007 8:33 PM:
<snip>
That was the one bit I didn't test.;-)
you have focussed on an obvious bug and completely failed to address
the actual question.
The "bug" being your total inability to write a script?
I wrote it and it worked, or didnt work in IE.
What I posted was a demonstrative snippet that I got wrong and didn't
test.
The point being that if you have a function and a variable with the
same name, and do a getElementsbyName on the form, in firefox and
opera the input is the zero'th element of the returned array, in IE
the browser throws an error if you access the zeroth element.
And I can't duplicate that. This code:
<script type="text/javascript">
function foo()
{
var dung;
dung=document.getElementsByName('foo')[0].value;
alert(dung);
}
</script>
<input type="hidden" name="foo" value="shite">
<button onclick="foo()">Test</button>
When I click that button in IE7, I get - "shite". No error, no problems,
it gives me what I expected. The only things I changed from your
original code was changing Alert to alert. So, what version of IE are
you getting that error in? And, what is the error message?
Ie6 and 7. 'error on page'. In the status bar at the bottom.

When I changed the variable names to make the functions NOT have the
same name as the variables, it worked.

I can see that you are talking about the same thing not understanding
each other...
OP puts his/her Javasript INSIDE the body (NOT in head) before the
input field. So when he/she runs the script
document.getElementsByName('foo')[0] refers to the FUNCTION foo and
generates the error trying to access attribute value which function
WILL NOT have...
Everybody else assumes that Javascript is included in head of the page
and so do NOT experience any errors.
Ah. You HAVE explained it. Except why it works in firefox, but not in IE6.
 
T

The Natural Philosopher

Henry said:
GArlington said the following on 10/18/2007 9:51 AM:
So when he/she runs the script document.getElementsByName('foo')[0]
refers to the FUNCTION foo and generates the error trying to access
attribute value which function WILL NOT have...
No, it doesn't. The function is not executed until you click on the
bold text in the original, the button in the latter. And, before
it is dreamed about, changing it back to a bold element didn't
make any difference either.

The code will not error on IE until someone puts the form elements
into a FORM element. At that point IE augments the scope chain of the
function that it creates from the intrinsic event attribute, and it
places the FORM element on that augmented scope chain. Thus when the
Identifier - foo - is resolved against this augmented scope chain an
object on the chain above the global object is found to have a 'foo'
property and the resulting value of - foo - is the value of that
property of the FORM element and not the global function.

After reading that 3 times, it almost makes sense.

It certainly is an odd proposition that moving the code into the HEAD
would make any difference.
I wouldnt know. My interest is avoiding pitfalls. Not becoming an expert
in a language I really think is rather badly written.

When I started, there was code that was easy to read and code that was
hard to understand.

Then suddenly everyone started waffling about 'structured programming'.
After a while I realized this was an academic term for what we had all
been doing anyway. Making code easy to read.

The the latest bollocks was 'object oriented'. But of course with
backwards compatiblity to procedural. Oh and lets throw typing away cots
its too hard for numpties.

Result is a fucking plate of spaghetti where to change three bytes in a
browser takes in interpreted variable name with more dots than a
golfball covered in brackets and completely impenetrable, since its
scope is implementation dependent, and, worse depends on where it sits
in some HTML as well.

I am coming to realise that Javascript is possibly the worst language I
have ever had to program in.
 
T

The Natural Philosopher

beegee said:
There is a bug in the implementation of getElementsByName in IE 6 and
7. The symptoms differ but your's are most like related. Read about
it and a fix, here:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1843487&SiteID=1&pageid=0

And please stop whining about javascript. Go to Yahoo User Interface
pages and watch Crockford's video on Advanced Javascript. Right now
you are looking at about 10% of the language and calling it crap. It
makes you look ignorant and trollish.

I'll whinge if I want to ;-)

I AM ignorant of javascript. But not of programming in general. All I
can say is that is is the worst language I've ever used from a personal
point of view.

And Microsnot has made a pigs ear of implementing it.
 
D

Doug Miller

I am coming to realise that Javascript is possibly the worst language I
have ever had to program in.

Never had any experience with RPG or LISP, I see... <g>
 
H

Henry

Henry wrote:

After reading that 3 times, it almost makes sense.



I wouldnt know. My interest is avoiding pitfalls.

That also seems odd, as you have so far reacted to most of the advice
you have been given here by disregarding it (often with the defence
that you know better).
Not becoming an expert in a language I really think is
rather badly written.

Some of the advice you were given was that you would be better off
writing your project with server-side code, you replied that you knew
better.
When I started, there was code that was easy to read and
code that was hard to understand.

Then suddenly everyone started waffling about 'structured
programming'. After a while I realized this was an academic
term for what we had all been doing anyway. Making code easy
to read.

The the latest bollocks was 'object oriented'. But of course
with backwards compatiblity to procedural. Oh and lets throw
typing away cots its too hard for numpties.

Loose typing is generally harder to program than strong typing. It
shifts responsibility for keeping track of typing from a complier to
the programmer and so requires a higher level of discipline from the
programmer if they don't what to end up not understanding what their
own code is doing.
Result is a fucking plate of spaghetti where to change three
bytes in a browser

You don't "change bytes" in javascript.
takes in interpreted variable name with more dots than a
golfball covered in brackets and completely impenetrable,
since its scope is implementation dependent, and, worse
depends on where it sits in some HTML as well.

Says who? The snippets of your code that you have posted are already
bizarre; who uses - getElementsByName - to reference a form control?
It seems that you are making a rod for your own back. And have already
spent any sympathy you may have received for you position.
I am coming to realise that Javascript is possibly the worst
language I have ever had to program in.

Then don't. It is not compulsory.
 
D

David Cox

The Natural Philosopher said:
I'll whinge if I want to ;-)

I AM ignorant of javascript. But not of programming in general. All I can
say is that is is the worst language I've ever used from a personal point
of view.

Right there with you, but somehow I don't think they are going to fix it
just for us..
 
T

The Natural Philosopher

Doug said:
Never had any experience with RPG or LISP, I see... <g>
RPG a very little.

I took one look at LISP and decided that a man has only so many years in
his life, and devoting them to LISP was not a way I chose to fill mine.

FORTH is anther one. But at least it has an excuse. Nothing compiles to
a smaller piece of code than FORTH.
 
T

The Natural Philosopher

Henry said:
That also seems odd, as you have so far reacted to most of the advice
you have been given here by disregarding it (often with the defence
that you know better).


Some of the advice you were given was that you would be better off
writing your project with server-side code, you replied that you knew
better.

I do know what i want to achieve: Ive had advice ranging fom 'do it
server side ' (most of it is) to 'do it all in javascrip' which is as I
am coming to understand, bad advice.
Loose typing is generally harder to program than strong typing. It
shifts responsibility for keeping track of typing from a complier to
the programmer and so requires a higher level of discipline from the
programmer if they don't what to end up not understanding what their
own code is doing.
Precisely.

You don't "change bytes" in javascript.

All programs ever do is 'change bytes' mate. Or did that one pass you by?

The soundest comment I ever received from one of the tighest machine
code programmers ever was 'languages schamguages: its all just bits, in
silicon'

Advice that the language junkies fail to heed.

A programming language is a *tool* to organize a sequence of inputs into
a unique and specified series of outputs. To do that it reads bytes, and
changes bytes, and outputs bytes (or bits or words) to something humans
interact with.

This oe is in my opinion a singularly bad one: its fashionably object
oriented, but its been cobbled together and it has no systematic
coherence: At lest with something like C or Fortran, there existed a
book or set of standards which defined the language.

Says who? The snippets of your code that you have posted are already
bizarre; who uses - getElementsByName - to reference a form control?

I do. I could have used id I guess..
It seems that you are making a rod for your own back. And have already
spent any sympathy you may have received for you position.


Then don't. It is not compulsory.
Sadly, if you want to get a browser to do tricks, it is.
 
T

The Natural Philosopher

David said:
Right there with you, but somehow I don't think they are going to fix it
just for us..

No. I begin to see what my friend said when he muttered darkly -
'Javascrpt: Active X - XML - sod that - I leave that to the geeks that
work for me: broswr development is a nightmare. Its no good implementing
to the specifications - none of the other browsers do.'

;-)
 
D

David Mark

Henry wrote: [snip]
Says who? The snippets of your code that you have posted are already
bizarre; who uses - getElementsByName - to reference a form control?

I do. I could have used id I guess..

And that would be silly as well. Why don't you try learning about DOM
implementations before attempting to manipulate them with JavaScript?
That will save you a lot of future frustration.
 
P

Peter Michaux

I'll whinge if I want to ;-)

I AM ignorant of javascript. But not of programming in general. All I
can say is that is is the worst language I've ever used from a personal
point of view.

You post to comp.lang.javascript asking for help with an untested
example. That makes it difficult for someone to understand your
problem.

You berated a respondent, who was generously volunteering his time,
for pointing out a bug in your untested example where the respondent
reasonably assumed you intended what you posted.

In self admitted ignorance, you berate the language that is the
subject of this group. The majority of regular posters to the group
understand and like the language. It is better than you think. They
have also read more than enough complaints from ignoramuses like you
about the language before and don't really want to hear it again. You
have not exactly won over anyone's affection with this sort of ranting
and reduced your chance of receiving help.

Sure you are able to vent or say anything you want on
comp.lang.javascript as it is a public group; however, if you want to
vent in public don't expect any help from the regulars. You will
likely be ignored.

My advice is to either apologize or change your screen name and start
again recognizing that the people here are generously volunteering
their time to help you. That is a really amazing thing they are
willing to do for you. They don't know you and have better things to
do I am sure.

Peter
 
T

Tim Streater

The Natural Philosopher said:
Henry said:
GArlington said the following on 10/18/2007 9:51 AM:
So when he/she runs the script document.getElementsByName('foo')[0]
refers to the FUNCTION foo and generates the error trying to access
attribute value which function WILL NOT have...
No, it doesn't. The function is not executed until you click on the
bold text in the original, the button in the latter. And, before
it is dreamed about, changing it back to a bold element didn't
make any difference either.

The code will not error on IE until someone puts the form elements
into a FORM element. At that point IE augments the scope chain of the
function that it creates from the intrinsic event attribute, and it
places the FORM element on that augmented scope chain. Thus when the
Identifier - foo - is resolved against this augmented scope chain an
object on the chain above the global object is found to have a 'foo'
property and the resulting value of - foo - is the value of that
property of the FORM element and not the global function.

After reading that 3 times, it almost makes sense.

It certainly is an odd proposition that moving the code into the HEAD
would make any difference.
I wouldnt know. My interest is avoiding pitfalls. Not becoming an expert
in a language I really think is rather badly written.

When I started, there was code that was easy to read and code that was
hard to understand.

Then suddenly everyone started waffling about 'structured programming'.
After a while I realized this was an academic term for what we had all
been doing anyway. Making code easy to read.

Well, to be fair, not everyone had been doing that. It takes discipline
to make code easy to read, and easy to maintain, particularly if you
expect it to be part of a bigger package that will be handed over to
others to look after. While at CERN, I once saw a FORTRAN subroutine
written by a physicist for an analysis program. It was 50 pages of code,
when printed out on the fanfold paper of the time.

I had little confidence that it was fit for purpose.

Hardest part is prevent programmers being too clever and getting them to
document what they are doing.
The the latest bollocks was 'object oriented'. But of course with
backwards compatiblity to procedural. Oh and lets throw typing away cots
its too hard for numpties.

Result is a fucking plate of spaghetti where to change three bytes in a
browser takes in interpreted variable name with more dots than a
golfball covered in brackets and completely impenetrable, since its
scope is implementation dependent, and, worse depends on where it sits
in some HTML as well.

I am coming to realise that Javascript is possibly the worst language I
have ever had to program in.

Your post made me chuckle and its no bad thing for pompous language
types to be needled from time to time. But I don't think JS *quite*
deserves this level of opprobrium.

Perl, on the other hand, does. That's a much shittier language than JS,
so feel free to mosey on over to their NG.

And remember: poor programmers can write FORTRAN in any language.
 
T

Tim Streater

[snip]
The soundest comment I ever received from one of the tighest machine
code programmers ever was 'languages schamguages: its all just bits, in
silicon'

Has this guy ever written in anything other than machine code? If not, I
wouldn't pay any attention to him whatever.
 

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,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top