Variable Substitution

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dennis Marks

    #1

    Variable Substitution

    In the following function:

    function showMenu(theMen u, theAction) {
    if (document.getEl ementById)
    {document.getEl ementById(theMe nu).style.visib ility = theAction;}
    else {if (document.layer s) {document.theMe nu.visibility = theAction;}
    else {document.all.t heMenu.style.vi sibility = theAction;}}
    }

    Will the passed value of theMenu be substituted in the last 2 lines or
    will it use "theMenu" as the value?

    The following words are in the last line:
    document
    all
    theMenu
    style
    visibility

    Since all are reserved words except for "theMenu" is that term
    changable?

    Do I have to use the following:
    eval("document. all."+theMenu+" .style.visibili ty")

    I have no way to test it.

    --
    Dennis Marks

    To reply change none to dcsi.


    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 100,000 Newsgroups - 19 Different Servers! =-----
  • Ivo

    #2
    Re: Variable Substitution

    "Dennis Marks" asked[color=blue]
    > In the following function:
    >
    > function showMenu(theMen u, theAction) {
    > if (document.getEl ementById)
    > {document.getEl ementById(theMe nu).style.visib ility = theAction;}
    > else {if (document.layer s) {document.theMe nu.visibility = theAction;}
    > else {document.all.t heMenu.style.vi sibility = theAction;}}
    > }
    >
    > Will the passed value of theMenu be substituted in the last 2 lines or
    > will it use "theMenu" as the value?[/color]

    The latter.
    [color=blue]
    > Do I have to use the following:
    > eval("document. all."+theMenu+" .style.visibili ty")[/color]

    No. Try document.all[theMenu].style.etc.
    with square brackets, no eval and no quotes.
    HTH
    Ivo


    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Variable Substitution

      Dennis Marks <denmarks@none. net> writes:
      [color=blue]
      > In the following function:
      >
      > function showMenu(theMen u, theAction) {
      > if (document.getEl ementById)
      > {document.getEl ementById(theMe nu).style.visib ility = theAction;}
      > else {if (document.layer s) {document.theMe nu.visibility = theAction;}
      > else {document.all.t heMenu.style.vi sibility = theAction;}}
      > }
      >
      > Will the passed value of theMenu be substituted in the last 2 lines or
      > will it use "theMenu" as the value?[/color]

      It will use the literal text "theMenu", since you are not writing a
      variable.
      [color=blue]
      >
      > The following words are in the last line:
      > document
      > all
      > theMenu
      > style
      > visibility
      >
      > Since all are reserved words except for "theMenu" is that term
      > changable?[/color]

      None of the words are reserved in the Javascript language. The reserved
      words are at the end of this page:
      <URL:http://www.crockford.c om/javascript/survey.html>
      (Hmm, of the six ways a reserved word can't be used, I think the first
      should be "As an *unquoted* name in linteral object notation")

      The identifiers have the following meaning:

      - The variable "document" refers to the variable of that name. This is
      usually the global variable, i.e., a property of the global object with
      that name. The value of the variable is used.

      - The identifier "all" is used as part of a property accessor (due to
      the "."), and is not interpreted as a variable. The name of the
      identifier is used. The document object of some browsers has a
      property called "all", so you find that.

      - The identifier "theMenu" is used as part of a property accessor (due
      to the "."), and is not interpreted as a variable. The name of the
      identifier is used. The object referenced by "document.a ll" may, or
      may not, have a property of that name.

      etc.
      [color=blue]
      > Do I have to use the following:
      > eval("document. all."+theMenu+" .style.visibili ty")[/color]

      Never. Never use eval, you don't need it, and the alternatives are
      both safer and more efficient.
      <URL:http://jibbering.com/faq/#FAQ4_40>

      Use:
      document.all[theMenu].style.visibili ty
      <URL:http://jibbering.com/faq/#FAQ4_39>

      In this notation, the part inside the [...] is an *expression*. In
      this case it is a expression consisting of a single variable, but
      it will be evaluated and its value used, not its name.
      [color=blue]
      > I have no way to test it.[/color]

      No browser at all?

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Dennis Marks

        #4
        Re: Variable Substitution

        In article <n04dbn3z.fsf@h otpop.com>, Lasse Reichstein Nielsen
        <lrn@hotpop.com > wrote:
        [color=blue]
        > Dennis Marks <denmarks@none. net> writes:
        >[color=green]
        > > In the following function:
        > >
        > > function showMenu(theMen u, theAction) {
        > > if (document.getEl ementById)
        > > {document.getEl ementById(theMe nu).style.visib ility = theAction;}
        > > else {if (document.layer s) {document.theMe nu.visibility = theAction;}
        > > else {document.all.t heMenu.style.vi sibility = theAction;}}
        > > }
        > >
        > > Will the passed value of theMenu be substituted in the last 2 lines or
        > > will it use "theMenu" as the value?[/color]
        >[/color]
        <snip>
        [color=blue]
        > Use:
        > document.all[theMenu].style.visibili ty
        > <URL:http://jibbering.com/faq/#FAQ4_39>
        >
        > In this notation, the part inside the [...] is an *expression*. In
        > this case it is a expression consisting of a single variable, but
        > it will be evaluated and its value used, not its name.
        >[color=green]
        > > I have no way to test it.[/color]
        >
        > No browser at all?
        >[/color]

        In my browsers only the statement immediately after the "if" is run.
        The "else" portions are for other browsers. At least that is my
        understanding. Please inform me if I am incorrect. I would like to
        simplify it if possible.

        The revised function is:

        function showMenu(theMen u, theAction) {
        if (document.getEl ementById)
        {document.getEl ementById(theMe nu).style.visib ility = theAction;}
        else {if (document.layer s) {document[theMenu].visibility =
        theAction;}
        else {document[theMenu].style.visibili ty = theAction;}}
        }

        --
        Dennis Marks

        To reply change none to dcsi.


        -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
        http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
        -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

        Comment

        • Robert

          #5
          Re: Variable Substitution

          In article <12052004153248 4629%denmarks@n one.net>,
          Dennis Marks <denmarks@none. net> wrote:
          [color=blue]
          > In my browsers only the statement immediately after the "if" is run.
          > The "else" portions are for other browsers. At least that is my
          > understanding. Please inform me if I am incorrect. I would like to
          > simplify it if possible.
          >
          > The revised function is:
          >
          > function showMenu(theMen u, theAction) {
          > if (document.getEl ementById)
          > {document.getEl ementById(theMe nu).style.visib ility = theAction;}
          > else {if (document.layer s) {document[theMenu].visibility =
          > theAction;}
          > else {document[theMenu].style.visibili ty = theAction;}}
          > }[/color]

          You could look in a public library for some books on IE with a cd-rom in
          them. Look for IE 4.5.

          You could comment out the code for newer browsers and let the new IE run
          with the old code.

          Robert

          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: Variable Substitution

            Dennis Marks <denmarks@none. net> writes:
            Dennis Marks <denmarks@none. net> writes:
            [color=blue]
            > In my browsers only the statement immediately after the "if" is run.[/color]

            Then get some more browsers!

            Netscape 2 (JavaScript 1.0):
            Netscape 3 (JavaScript 1.1):
            Netscape 4.00-4.05 (JavaScript 1.2):
            Netscape 4.06-4.80 (JavaScript 1.3):
            Netscape 7 (JavaScript 1.4):
            <URL:http://wp.netscape.com/download/archive.html>
            Mozilla FireFox (same engine as Netscape 7, just more recent version):
            <URL:http://www.mozilla.org/products/firefox/>

            IE 3 (JScript 1.0):
            IE 4 (JScript 3.0):
            IE 5.01 (JScript 5.0):
            IE 5.5 (JScript 5.5):
            <URL:http://www.quirksmode. org/browsers/multipleie.html >

            Opera v. 3.62, 5.12, 6.06, 7.11 (soon 7.23):
            <URL: http://arc.opera.com/pub/opera/win/ >
            Opera 7.5:
            <URL: http://www.opera.com/download/ >
            (I have a version 4 too, if you want it)

            Earlier verions of these browsers didn't support Javascript.

            If that's not enough, here is the rest:
            <URL:http://browsers.evolt. org/>

            /L
            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            • Ivo

              #7
              Re: Variable Substitution

              "Dennis Marks" wrote[color=blue]
              > In my browsers only the statement immediately after the "if" is run.
              > The "else" portions are for other browsers. At least that is my
              > understanding. Please inform me if I am incorrect. I would like to
              > simplify it if possible.
              >
              > The revised function is:
              >
              > function showMenu(theMen u, theAction) {
              > if (document.getEl ementById)
              > {document.getEl ementById(theMe nu).style.visib ility = theAction;}
              > else {if (document.layer s) {document[theMenu].visibility =
              > theAction;}
              > else {document[theMenu].style.visibili ty = theAction;}}
              > }[/color]

              There are two brackets too many in this function. The one just before "if
              (document.layer s)" and the last one should be removed. Currently the logic
              looks like:

              if (foo) { x=1; } else { if (bar) x=2; } else { x=3; }

              which is not logical. I suspect you meant:

              if (foo) { x=1; } else if (bar) { x=2; } else{ x=3; }


              If you don't have all the browsers you aim to support, there is little in
              scripting for them. Even the best need to test. To test stuff for older
              browsers, you can usually turn the "if" around (temporarily) with a ! in the
              right place.
              HTH
              Ivo


              Comment

              • Dennis Marks

                #8
                Re: Variable Substitution

                In article <pt98axoz.fsf@h otpop.com>, Lasse Reichstein Nielsen
                <lrn@hotpop.com > wrote:
                [color=blue]
                > Dennis Marks <denmarks@none. net> writes:
                > Dennis Marks <denmarks@none. net> writes:
                >[color=green]
                > > In my browsers only the statement immediately after the "if" is run.[/color]
                >
                > Then get some more browsers!
                >
                > Netscape 2 (JavaScript 1.0):
                > Netscape 3 (JavaScript 1.1):
                > Netscape 4.00-4.05 (JavaScript 1.2):
                > Netscape 4.06-4.80 (JavaScript 1.3):
                > Netscape 7 (JavaScript 1.4):
                > <URL:http://wp.netscape.com/download/archive.html>
                > Mozilla FireFox (same engine as Netscape 7, just more recent version):
                > <URL:http://www.mozilla.org/products/firefox/>
                >
                > IE 3 (JScript 1.0):
                > IE 4 (JScript 3.0):
                > IE 5.01 (JScript 5.0):
                > IE 5.5 (JScript 5.5):
                > <URL:http://www.quirksmode. org/browsers/multipleie.html >
                >
                > Opera v. 3.62, 5.12, 6.06, 7.11 (soon 7.23):
                > <URL: http://arc.opera.com/pub/opera/win/ >
                > Opera 7.5:
                > <URL: http://www.opera.com/download/ >
                > (I have a version 4 too, if you want it)
                >
                > Earlier verions of these browsers didn't support Javascript.
                >
                > If that's not enough, here is the rest:
                > <URL:http://browsers.evolt. org/>
                >
                > /L[/color]

                Don't assume I am on a PC. I am on a Mac using system 9. I am not able
                to use any of the latest browsers. They require system X and I don't
                wish to invest in an upgrade since I might soon switch to a PC. That is
                why I asked if the function was correct.

                --
                Dennis Marks

                To reply change none to dcsi.


                -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
                http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
                -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

                Comment

                • Dennis Marks

                  #9
                  Re: Variable Substitution

                  In article <40a3db2d$0$659 13$a344fe98@new s.wanadoo.nl>, Ivo
                  <no@thank.you > wrote:
                  [color=blue]
                  > "Dennis Marks" wrote[color=green]
                  > > In my browsers only the statement immediately after the "if" is run.
                  > > The "else" portions are for other browsers. At least that is my
                  > > understanding. Please inform me if I am incorrect. I would like to
                  > > simplify it if possible.
                  > >
                  > > The revised function is:
                  > >
                  > > function showMenu(theMen u, theAction) {
                  > > if (document.getEl ementById)
                  > > {document.getEl ementById(theMe nu).style.visib ility = theAction;}
                  > > else {if (document.layer s) {document[theMenu].visibility =
                  > > theAction;}
                  > > else {document[theMenu].style.visibili ty = theAction;}}
                  > > }[/color]
                  >
                  > There are two brackets too many in this function. The one just before "if
                  > (document.layer s)" and the last one should be removed. Currently the logic
                  > looks like:
                  >
                  > if (foo) { x=1; } else { if (bar) x=2; } else { x=3; }
                  >
                  > which is not logical. I suspect you meant:
                  >
                  > if (foo) { x=1; } else if (bar) { x=2; } else{ x=3; }
                  >
                  >
                  > If you don't have all the browsers you aim to support, there is little in
                  > scripting for them. Even the best need to test. To test stuff for older
                  > browsers, you can usually turn the "if" around (temporarily) with a ! in the
                  > right place.
                  > HTH
                  > Ivo
                  >
                  >[/color]
                  Thank you for your help. I am just creating a general division
                  show/hide function based on information that I have found in this
                  group.
                  I assume that the 3 methods used to show/hide are used by different
                  browsers. My browsers use getElementById.
                  The two forms of document{theMen u] do not work on my browsers. My
                  question is are they valid and are they necessary? I'll remove it if it
                  is only necessary for a few old browsers.

                  --
                  Dennis Marks

                  To reply change none to dcsi.


                  -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
                  http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
                  -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

                  Comment

                  Working...