Select, Option, innerHTML,Internet Explorer, Firefox, bug

P

Phper

I wrote a block of Javascript code, it works fine in Firefox. But it
doesn't work in Internet Explorer.

function SelectDistrict(argument)
{ if(argument==undefined)
{}
else
{
var _myhtml ="<option value=\"\">select a locationon</option>";
var tempInt = parseInt(argument);
$('#inputcity').hide();
var i=1;

while(i<=51)
{
tempInt=tempInt+100;

if(county[tempInt]==undefined)
{
_myhtml+="<option value=99>All</option>";
var selectCity = document.getElementById('city');
selectCity.style.display = '';
selectCity.innerHTML = _myhtml;
break;

}
else {
_myhtml+="<option value=\""+tempInt+"\">"+county[tempInt]+"</
option>";
i++;

}
}
}
}

How can I make it work both in Firefox and Internet Explorer?
 
S

SAM

Le 9/12/09 5:07 AM, Phper a écrit :
I wrote a block of Javascript code, it works fine in Firefox. But it
doesn't work in Internet Explorer.

function SelectDistrict(argument)
{ if(argument==undefined)

always begin with the value you know it is known by JS :

if ( undefined == argument )

and much more better :

if( typeof argument == 'undefined')


function SelectDistrict(argument)
{ if( typeof argument != 'undefined')
{
var _myhtml ='<option value="">select a location<\/option>\n\t';
var tempInt = parseInt(argument);
$('#inputcity').hide();
var i=1;
while(i<=51)
{
tempInt += 100;
if( undefined == county[tempInt])
{
_myhtml += '<option value="99">All<\/option>\n\t';
var selectCity = document.getElementById('city');
selectCity.style.display = '';
selectCity.innerHTML = _myhtml;
break;
}
else
{
_myhtml += '<option value="'+tempInt+'">'+county[tempInt]+
'<\/option>\n\t';
i++;
}
}
}
}


Variante not using innerHTML :

function SelectDistrict(argument)
{ if( typeof argument != 'undefined')
{
var selectCity = document.getElementById('city');
if(selectCity)
{
selectCity.length = 0;
var tempInt = parseInt(argument);
$('#inputcity').hide();
var i = 0;
var o, v, t;
while(i<=51)
{
if(i == 0) { v = ''; t = 'select a location'; }
else
{
if(undefined == county[tempInt])
{ v = '99'; t = 'All'; }
else
{
tempInt += 100;
v = tempInt;
t = county[tempInt];
}
}
o = new Option(t, v);
selectCity[selectCity.length] = o;
if(undefined == county[tempInt]) break;
i++;
}
}
else alert('The select "City" doesn\'t exist');
}
}
 
L

Lasse Reichstein Nielsen

SAM said:
Le 9/12/09 5:07 AM, Phper a écrit :

always begin with the value you know it is known by JS :

if ( undefined == argument )

Why is that any better? Both "argument" and "undefined" are variables
that is known to be defined.
(Well, actually, "undefined" may not have been deleted, or in very
early browsers it wasn't even there).

If it's to avoid that a "=="->"=" typo breaks anything, this way around
is actually much worse, since it might overwrite the global "undefined"
variable, and screw up everyting instead of just a local variable.
and much more better :

if( typeof argument == 'undefined')

That works too, but isn't really any better than (argument === undefined)
when you know that "argument" is a variable and you just want to test its
value.
It's much better when you don't know if the variable exists, e.g.,
typeof foo[x].prop == "undefined"
where foo[x] might or might not exist.

/L
 
S

SAM

Le 9/12/09 11:48 AM, Lasse Reichstein Nielsen a écrit :
Why is that any better? Both "argument" and "undefined" are variables
that is known to be defined.

Though that undefined was so well known as null or false for instance.

argument if not set when calling the function will be unknown

Don't think that argument can be known as true, false, and so
(Well, actually, "undefined" may not have been deleted, or in very
early browsers it wasn't even there).

If it's to avoid that a "=="->"=" typo breaks anything,

It's just to avoid to break the function following that 'argument' is
not known (undefined)
if the 1st term of the comparison is OK normally that can't give an
error when the second doesn't exist

Just, finally that doesn't work as I expected (in old browser), anyway
'undefined' seems to be unknown too :-(
That works too, but isn't really any better than (argument === undefined)

I don't know
I'll try

Tried : your code doesn't work ... argument is undefined !
and same error with (undefined === argument) ... undefined is undefined

The way by typeof is OK

Well, tests made with an 'old' browser.
It's much better when you don't know if the variable exists, e.g.,
typeof foo[x].prop == "undefined"
where foo[x] might or might not exist.

Well, at least you're OK with me on one point.
 
L

Lasse Reichstein Nielsen

SAM said:
Le 9/12/09 11:48 AM, Lasse Reichstein Nielsen a écrit :

Though that undefined was so well known as null or false for instance.

Almost, but not entirely. "false" and "null" are keywords, but
"undefined" is just the name of a global variable, and its value can
be overwritten. It can't be deleted, though.
argument if not set when calling the function will be unknown

The "argument" variable always exists in the body of the function.
If no corresponding argument was passed, it will have the value
"undefined", but it still exists.
Don't think that argument can be known as true, false, and so

The variable "argument" is statically defined in the scope of the
function body, so it's known to exists. So is undefined, which
is a global variablle.
It's just to avoid to break the function following that 'argument' is
not known (undefined)
if the 1st term of the comparison is OK normally that can't give an
error when the second doesn't exist

There is no difference between the first and second operand of the
equality operator (except that one is evaluated before the other).
Just, finally that doesn't work as I expected (in old browser), anyway
'undefined' seems to be unknown too :-(

Correct. There was no global "undefined" variable back then.
I don't know
I'll try

Tried : your code doesn't work ... argument is undefined !

Inside "function(argument) { ... }"?
and same error with (undefined === argument) ... undefined is undefined

Yes, you need to define undefined first. A typical way would be
window.undefined = window.undefined;
The way by typeof is OK

Well, tests made with an 'old' browser.

I admit that I don't care about, say, Netscape 4 and IE 5 any more.
More care, much more, needs to be taken if they need to be supported.

/L
 
D

Dr J R Stockton

Sat said:
Why is that any better? Both "argument" and "undefined" are variables
that is known to be defined.
(Well, actually, "undefined" may not have been deleted, or in very
early browsers it wasn't even there).


The idea of putting a constant first is that the very easy error of
typing = instead of == will be rapidly detected.

In Firefox 3.0.13, undefined is initially defined as undefined.

However,

if (undefined = 5) alert(99)
alert(undefined + 3)

alerts first 99 then 8. Therefore, undefined can be redefined, and need
not then be defined as undefined. Same result in Opera 9.64 and IE 8.

Calling it undefined was really silly, since that confuses talking about
it in English. It should have been called incognitus, the meaning of
which is obvious and of which Google finds only 107,000 as opposed to
48,800,000.

However, null seems actually immutable.

A work-round may be to use

function undefined() { }

if (undefined() = 5) alert(99)
alert(undefined + 3)

but there's no protection against forgetting the () .
 
S

SAM

Le 9/12/09 10:29 PM, Lasse Reichstein Nielsen a écrit :
There is no difference between the first and second operand of the
equality operator (except that one is evaluated before the other).

And ? isn't it almost what I try to say ?
if the 1st term (the term evaluated in 1st) exists in if( x == y)
hop! no error
Inside "function(argument) { ... }"?

the function is called without nothing as 'argument'
onclick="myFunction()"
I admit that I don't care about, say, Netscape 4 and IE 5 any more.

Eh oui ! de + en + they are forgotten.
 
T

Thomas 'PointedEars' Lahn

Lasse said:
Yes, you need to define undefined first. A typical way would be
window.undefined = window.undefined;

One of the proper ways, though, is

var undefined = void 0;

in the global execution context.


PointedEars
 
J

John G Harris

alerts first 99 then 8. Therefore, undefined can be redefined, and need
not then be defined as undefined. Same result in Opera 9.64 and IE 8.

Calling it undefined was really silly, since that confuses talking about
it in English.

If you go back to last century you'll find that several popular web
sites did
var undefined;
so that they could assign the value undefined to other variables. Then
Netscape decided to help them by predefining it in the browser. They
couldn't make it a reserved word as that would have killed the web sites
they were trying to help. The variable was called 'undefined' because
that's the sort of thing that programmers do, using all the foresight
that getYear demonstrated.

It should have been called incognitus,

You're joking. In the USA ?

the meaning of
which is obvious and of which Google finds only 107,000 as opposed to
48,800,000.

Surely incognitus means unknown, not undefined.

However, null seems actually immutable.
<snip>

null is a reserved word, so cannot be used as an identifier.

John
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
8924D9443D28E23ED5CD>, Sun, 13 Sep 2009 15:09:23, John G Harris
If you go back to last century you'll find that several popular web
sites did
var undefined;
so that they could assign the value undefined to other variables.

Personally, I use var U within a routine that needs it. It's easier
to spell.
You're joking. In the USA ?

Even a small Webster has "bus", so words of Latin origin are allowed.
And there's nothing wrong with using an unknown word to mean unknown.
The essential point is that it should NOT be any English word.
Surely incognitus means unknown, not undefined.

I only have a small Latin dictionary; in it, undefined is undefined. Do
you have a Lewis and Short, or an OLD, handy? If you can read Latin
readily, you could be of assistance with Clavius.
 
J

John G Harris

In comp.lang.javascript message <[email protected]
8924D9443D28E23ED5CD>, Sun, 13 Sep 2009 15:09:23, John G Harris

Even a small Webster has "bus", so words of Latin origin are allowed.
And there's nothing wrong with using an unknown word to mean unknown.
The essential point is that it should NOT be any English word.
<snip>

There's a big difference between unknown and undefined. The next
president of the USA is unknown; the current king of the USA is
undefined.

I only have a small Latin dictionary; in it, undefined is undefined. Do
you have a Lewis and Short, or an OLD, handy? If you can read Latin
readily, you could be of assistance with Clavius.

Look up incognito in an English dictionary.

John
 

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,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top