With clause syntax errors but how do I fix it?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Powell

    With clause syntax errors but how do I fix it?

    <script>
    <!--

    function isValidAlert() {
    for (var i = 0; i < document.alertF orm.length; i++) {
    with (document.alert Form.elements[i]) {
    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.alertF orm.elements[i].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


  • Laurent Bugnion, GalaSoft

    #2
    Re: With clause syntax errors but how do I fix it?

    Hi,

    Phil Powell wrote:
    [color=blue]
    > <script>
    > <!--
    >
    > function isValidAlert() {
    > for (var i = 0; i < document.alertF orm.length; i++) {
    > with (document.alert Form.elements[i]) {
    > 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.alertF orm.elements[i].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[/color]

    "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.theNa meOfTheForm.the NameOfTheText;

    myText.value = "Hello world";

    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

    Comment

    • Laurent Bugnion, GalaSoft

      #3
      Re: With clause syntax errors but how do I fix it? (correction)

      Correction, I was wrong. See below.

      Laurent Bugnion, GalaSoft wrote:
      [color=blue]
      > Hi,
      >
      > Phil Powell wrote:
      >[color=green]
      >> <script>
      >> <!--
      >>
      >> function isValidAlert() {
      >> for (var i = 0; i < document.alertF orm.length; i++) {
      >> with (document.alert Form.elements[i]) {
      >> 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.alertF orm.elements[i].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[/color]
      >
      >
      > "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.theNa meOfTheForm.the NameOfTheText;
      >
      > myText.value = "Hello world";
      >
      > Laurent[/color]

      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

      Comment

      • Phil Powell

        #4
        Re: With clause syntax errors but how do I fix it? (correction)

        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.alertF orm.elements[i].options[document.alertF orm.elements[i].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, GalaSoft" <galasoft-LB@bluewin_NO_S PAM.ch> wrote in
        message news:3f53877a_1 @news.bluewin.c h...[color=blue]
        > Correction, I was wrong. See below.
        >
        > Laurent Bugnion, GalaSoft wrote:
        >[color=green]
        > > Hi,
        > >
        > > Phil Powell wrote:
        > >[color=darkred]
        > >> <script>
        > >> <!--
        > >>
        > >> function isValidAlert() {
        > >> for (var i = 0; i < document.alertF orm.length; i++) {
        > >> with (document.alert Form.elements[i]) {
        > >> if (.name == "text" || .name == "password" || .name == "textarea")[/color][/color][/color]
        {[color=blue][color=green][color=darkred]
        > >> if (.value == "") {
        > >> alert("Please enter a " + .name);
        > >> focus();
        > >> return false;
        > >> }
        > >> } else if (.name == "select-one") {
        > >> if (.options[document.alertF orm.elements[i].selectedIndex].value[/color][/color][/color]
        ==[color=blue][color=green][color=darkred]
        > >> "") {
        > >> alert("Please select a " + .name);
        > >> return false;
        > >> }
        > >> }
        > >> }
        > >> }
        > >> }
        > >>
        > >> //-->
        > >> </script>
        > >>
        > >> I don't know the answer to this problem, I am getting syntax errors,[/color][/color][/color]
        and[color=blue][color=green][color=darkred]
        > >> "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[/color]
        > >
        > >
        > > "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.theNa meOfTheForm.the NameOfTheText;
        > >
        > > myText.value = "Hello world";
        > >
        > > Laurent[/color]
        >
        > 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
        >[/color]


        Comment

        • DU

          #5
          Re: With clause syntax errors but how do I fix it?

          Phil Powell wrote:
          [color=blue]
          > <script>
          > <!--
          >
          > function isValidAlert() {
          > for (var i = 0; i < document.alertF orm.length; i++) {
          > with (document.alert Form.elements[i])[/color]
          {[color=blue]
          > if (.name == "text" || .name == "password" || .name == "textarea") {[/color]

          This seems suspicious. I bet it would be rather:

          if (type == "text" || type == "password" || type == "textarea") {
          [color=blue]
          > if (.value == "")[/color]

          if(value == "")

          {[color=blue]
          > alert("Please enter a " + .name);
          > focus();
          > return false;
          > }
          > } else if (.name == "select-one") {
          > if (.options[document.alertF orm.elements[i].selectedIndex].value ==
          > "") {
          > alert("Please select a " + .name);
          > return false;[/color]

          Remove "." everywhere and try to make your code more compact.
          [color=blue]
          > }
          > }
          > }
          > }
          > }
          >
          > //-->
          > </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
          >
          >[/color]

          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:

          - Resources, help and tips for Netscape 7.x users and Composer
          - Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x


          Comment

          • DU

            #6
            Re: With clause syntax errors but how do I fix it? (correction)

            Phil Powell wrote:
            [color=blue]
            > 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.alertF orm.elements[i].options[document.alertF orm.elements[i].select
            > edIndex].value
            >
            > I think that is utterly unreadable myself![/color]

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

            var IteratedFormEle ment = document.alertF orm.elements[i];
            (...)

            if(IteratedForm Element.options[IteratedFormEle ment.selectedIn dex].value
            == "")
            (...)

            I was trying to come up with an[color=blue]
            > elegant and readable solution to parse through form elements for client-side
            > validation. :(
            >[/color]

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

            - Resources, help and tips for Netscape 7.x users and Composer
            - Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x


            Comment

            • Phil Powell

              #7
              Re: With clause syntax errors but how do I fix it? (correction)

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

              Phil

              "DU" <drunclear@hotR EMOVEmail.com> wrote in message
              news:bj02q2$bi1 $1@news.eusc.in ter.net...[color=blue]
              > Phil Powell wrote:
              >[color=green]
              > > Wow, damned one way or another.. that is awful!
              > >
              > > I don't know what is more unreadable, how you view a "with" clause, or[/color][/color]
              this:[color=blue][color=green]
              > >
              > >[/color][/color]
              document.alertF orm.elements[i].options[document.alertF orm.elements[i].select[color=blue][color=green]
              > > edIndex].value
              > >
              > > I think that is utterly unreadable myself![/color]
              >
              > Then just use a local variable to store each element (at the start of
              > your for loop). E.g.:
              >
              > var IteratedFormEle ment = document.alertF orm.elements[i];
              > (...)
              >
              > if(IteratedForm Element.options[IteratedFormEle ment.selectedIn dex].value
              > == "")
              > (...)
              >
              > I was trying to come up with an[color=green]
              > > elegant and readable solution to parse through form elements for[/color][/color]
              client-side[color=blue][color=green]
              > > validation. :(
              > >[/color]
              >
              > [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/doctorunc...e7Section.html
              >[/color]


              Comment

              • Janwillem Borleffs

                #8
                Re: With clause syntax errors but how do I fix it? (correction)

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

                "Phil Powell" <soazine@erols. com> schreef in bericht
                news:swM4b.9270 9$xf.26491@lake read04...[color=blue]
                > top-posting? HUH? English, please, or Svenska.
                >[/color]

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

                "Phil Powell" <soazine@erols. com> schreef in bericht
                news:swM4b.9270 9$xf.26491@lake read04...[color=blue]
                > top-posting? HUH? English, please, or Svenska.
                >[/color]

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


                JW



                Comment

                • Janwillem Borleffs

                  #9
                  Re: With clause syntax errors but how do I fix it? (correction)


                  "Phil Powell" <soazine@erols. com> schreef in bericht
                  news:AVL4b.9235 7$xf.24656@lake read04...[color=blue]
                  > 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.
                  >[/color]

                  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="doAler t()" value="Test">
                  </form>
                  </body>
                  </html>

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


                  JW



                  Comment

                  • Phil Powell

                    #10
                    Re: With clause syntax errors but how do I fix it? (correction)

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

                    Phil

                    "Janwillem Borleffs" <jwb@jwbfoto.de mon.nl> wrote in message
                    news:3f53b7aa$0 $28887$1b62eedf @news.euronet.n l...[color=blue]
                    >
                    > "Phil Powell" <soazine@erols. com> schreef in bericht
                    > news:AVL4b.9235 7$xf.24656@lake read04...[color=green]
                    > > 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.
                    > >[/color]
                    >
                    > 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>[/color]

                    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
                    [color=blue]
                    > </head>
                    >
                    > <body>
                    > <form>
                    > <input type="text" value="text" name="test">
                    > <input type="button" onClick="doAler t()" value="Test">
                    > </form>
                    > </body>
                    > </html>
                    >
                    > BTW, I don't subscribe to Laurent's point of view regarding the with[/color]
                    clause.[color=blue]
                    >
                    >
                    > JW
                    >
                    >
                    >[/color]


                    Comment

                    • Phil Powell

                      #11
                      Re: With clause syntax errors but how do I fix it? (correction)

                      Maybe it's a comp.lang.javas cript 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

                      "Janwillem Borleffs" <jwb@jwbfoto.de mon.nl> wrote in message
                      news:3f53b19e$0 $28907$1b62eedf @news.euronet.n l...[color=blue]
                      > Top-posting is putting your reply on top of the message or phrase you are
                      > replying to. As I'm doing now...
                      >
                      > "Phil Powell" <soazine@erols. com> schreef in bericht
                      > news:swM4b.9270 9$xf.26491@lake read04...[color=green]
                      > > top-posting? HUH? English, please, or Svenska.
                      > >[/color]
                      >
                      > -----------------
                      >
                      > "Phil Powell" <soazine@erols. com> schreef in bericht
                      > news:swM4b.9270 9$xf.26491@lake read04...[color=green]
                      > > top-posting? HUH? English, please, or Svenska.
                      > >[/color]
                      >
                      > This is the proper way of replying to a message by putting your reply
                      > beneath the message.
                      >
                      >
                      > JW
                      >
                      >
                      >[/color]


                      Comment

                      • Janwillem Borleffs

                        #12
                        Re: With clause syntax errors but how do I fix it? (correction)


                        "Phil Powell" <soazine@erols. com> schreef in bericht
                        news:HMO4b.9331 5$xf.73424@lake read04...[color=blue]
                        >
                        > 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".
                        >[/color]

                        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



                        Comment

                        • Phil Powell

                          #13
                          Re: With clause syntax errors but how do I fix it? (correction)

                          Never mind, you lost me completely.

                          Phil

                          "DU" <drunclear@hotR EMOVEmail.com> wrote in message
                          news:bj0f2t$g4g $1@news.eusc.in ter.net...[color=blue]
                          >
                          > http://www10.brinkster.com/doctorunc...e7Section.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[color=green]
                          > >
                          > > 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;[/color]
                          > and I[color=green]
                          > > with that? That is the conventional reply. I'd never think to look[/color][/color]
                          below[color=blue][color=green]
                          > > Maybe it's a comp.lang.javas cript thing but I don't get it. What's[/color][/color]
                          wrong[color=blue]
                          > Phil Powell wrote:
                          >
                          > [snipped]
                          >
                          >
                          >
                          >
                          >
                          >
                          >
                          >[/color]


                          Comment

                          • Phil Powell

                            #14
                            Re: With clause syntax errors but how do I fix it? (correction)

                            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" <jwb@jwbfoto.de mon.nl> wrote in message
                            news:3f53c17d$0 $28893$1b62eedf @news.euronet.n l...[color=blue]
                            >
                            > "Phil Powell" <soazine@erols. com> schreef in bericht
                            > news:HMO4b.9331 5$xf.73424@lake read04...[color=green]
                            > >
                            > > 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.[/color][/color]
                            VB[color=blue][color=green]
                            > > you would have "name" and ".name".
                            > >[/color]
                            >
                            > Yes it does, but you must define the variable as a property of the[/color]
                            function:[color=blue]
                            >
                            > 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.,[/color]
                            when[color=blue]
                            > my script encounters a pre-defined property 'name', then I shouldn't[/color]
                            define[color=blue]
                            > a variable with the same name. This is a good thing in my point of view.
                            >
                            >
                            > JW
                            >
                            >
                            >[/color]


                            Comment

                            • Jim Ley

                              #15
                              Re: With clause syntax errors but how do I fix it? (correction)

                              On Mon, 1 Sep 2003 18:12:13 -0400, "Phil Powell" <soazine@erols. com>
                              wrote:
                              [color=blue]
                              >That is a Java/CF/PHP solution moreso than Javascript. It appears
                              >Javascript is becoming more like Java every day!!![/color]

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

                              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.
                              --
                              comp.lang.javas cript FAQ - http://jibbering.com/faq/

                              Comment

                              Working...