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

Discussion in 'Javascript' started by Phper, Sep 12, 2009.

  1. Phper

    Phper Guest

    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?
     
    Phper, Sep 12, 2009
    #1
    1. Advertising

  2. Phper

    SAM Guest

    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')

    > {}
    > else



    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');
    }
    }


    --
    sm
     
    SAM, Sep 12, 2009
    #2
    1. Advertising

  3. SAM <> writes:

    > 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 )


    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
    --
    Lasse Reichstein Holst Nielsen
    'Javascript frameworks is a disruptive technology'
     
    Lasse Reichstein Nielsen, Sep 12, 2009
    #3
  4. Phper

    SAM Guest

    Le 9/12/09 11:48 AM, Lasse Reichstein Nielsen a écrit :
    > SAM <> writes:
    >
    >> 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.


    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 :-(

    >> and much more better :
    >>
    >> if( typeof argument == 'undefined')

    >
    > 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.

    --
    sm
     
    SAM, Sep 12, 2009
    #4
  5. SAM <> writes:

    > Le 9/12/09 11:48 AM, Lasse Reichstein Nielsen a écrit :
    >> SAM <> writes:
    >>
    >>> 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.

    >
    > 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.

    >>> and much more better :
    >>>
    >>> if( typeof argument == 'undefined')

    >> 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 !


    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
    --
    Lasse Reichstein Holst Nielsen
    'Javascript frameworks is a disruptive technology'
     
    Lasse Reichstein Nielsen, Sep 12, 2009
    #5
  6. In comp.lang.javascript message <>, Sat, 12 Sep
    2009 11:48:53, Lasse Reichstein Nielsen <> posted:
    >>
    >> 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).



    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 () .

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
    Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
    Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
     
    Dr J R Stockton, Sep 12, 2009
    #6
  7. Phper

    SAM Guest

    Le 9/12/09 10:29 PM, Lasse Reichstein Nielsen a écrit :
    > SAM <> writes:
    >
    >> 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).


    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

    >>>> if( typeof argument == 'undefined')
    >>> 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 !

    >
    > Inside "function(argument) { ... }"?


    the function is called without nothing as 'argument'
    onclick="myFunction()"

    >> Well, tests made with an 'old' browser.

    >
    > I admit that I don't care about, say, Netscape 4 and IE 5 any more.


    Eh oui ! de + en + they are forgotten.

    --
    sm
     
    SAM, Sep 13, 2009
    #7
  8. Lasse Reichstein Nielsen wrote:
    > SAM <> writes:
    >> 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;


    One of the proper ways, though, is

    var undefined = void 0;

    in the global execution context.


    PointedEars
    --
    Use any version of Microsoft Frontpage to create your site.
    (This won't prevent people from viewing your source, but no one
    will want to steal it.)
    -- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
     
    Thomas 'PointedEars' Lahn, Sep 13, 2009
    #8
  9. On Sat, 12 Sep 2009 at 22:03:19, in comp.lang.javascript, Dr J R
    Stockton wrote:

    <snip>
    >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
    --
    John Harris
     
    John G Harris, Sep 13, 2009
    #9
  10. In comp.lang.javascript message <830F0FF37FB96852AD0
    8924D9443D28E23ED5CD>, Sun, 13 Sep 2009 15:09:23, John G Harris
    <> posted:
    >On Sat, 12 Sep 2009 at 22:03:19, in comp.lang.javascript, Dr J R
    >Stockton wrote:


    >>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.


    Personally, I use var U within a routine that needs it. It's easier
    to spell.

    >>It should have been called incognitus,

    >
    >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.

    >>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.


    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.

    --
    (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
    Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
    Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.
     
    Dr J R Stockton, Sep 13, 2009
    #10
  11. On Sun, 13 Sep 2009 at 23:43:18, in comp.lang.javascript, Dr J R
    Stockton wrote:
    >In comp.lang.javascript message <830F0FF37FB96852AD0
    >8924D9443D28E23ED5CD>, Sun, 13 Sep 2009 15:09:23, John G Harris
    ><> posted:
    >>On Sat, 12 Sep 2009 at 22:03:19, in comp.lang.javascript, Dr J R
    >>Stockton wrote:


    <snip>
    >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.


    >>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.


    Look up incognito in an English dictionary.

    John
    --
    John Harris
     
    John G Harris, Sep 14, 2009
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ed Hauptman
    Replies:
    7
    Views:
    1,019
    Ed Hauptman
    Aug 7, 2009
  2. Jason
    Replies:
    3
    Views:
    117
    kaeli
    Apr 19, 2004
  3. McKirahan
    Replies:
    8
    Views:
    175
    McKirahan
    Oct 12, 2005
  4. Replies:
    1
    Views:
    161
  5. Replies:
    0
    Views:
    133
Loading...

Share This Page