Undefined variable error

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

    Undefined variable error

    Hi! Does anyone know why the onclick in the following popup menu gives
    the error:"Val is undefined"? Does it have something to do with the
    fact that it is called within the variable tablePop? Because it IS
    displayed properly as part of the popup text, where it is called
    outside the single quotation marks (see [***]). It is only in the
    onclick that it's causing problems. Who can help me?

    function dopopup(x,y) {
    var Val=event.srcEl ement.href1.sub string(0,(event .srcElement.hre f1.length-1));
    var Field=event.src Element.href2.s ubstring(1,(eve nt.srcElement.h ref2.length-1));
    var tablePop='';
    tablePop+='<TAB LE oncontextmenu=\ "return false\";>';
    tablePop+='<SCR IPT LANGUAGE="JavaS cript">\n';
    tablePop+='\n<!--\n';
    tablePop+='wind ow.onerror=null ;\n';
    tablePop+='/-->\n';
    tablePop+='<\/SCRIPT>\n';
    tablePop+='<TR> <TD ONCLICK="render Data(Val,Field) ;">&nbsp;Fil ter op
    '[***] + Field + ' is ' + Val +'</TD></TR>';
    tablePop+='<TR> <TD>&nbsp;Filte r op ' + Field + ' is NIET '+
    Val +'</TD></TR>';
    tablePop+='<\/TABLE>';
    var oPopupBody = oPopup.document .body;
    oPopupBody.inne rHTML = tablePop;
    oPopup.show(x, y, 140, 220, document.body);
    }

    function renderData(filt erValue,filterF ield)
    {
    alert(filterVal ue);
    alert(filterFie ld);
    }
  • Stuart Palmer

    #2
    Re: Undefined variable error

    > var
    Val=event.srcEl ement.href1.sub string(0,(event .srcElement.hre f1.length-1));

    have you tried alerting the value of Val ? to see what it holds?

    thx stu

    "Sharon" <esdeees@hotmai l.com> wrote in message
    news:2b13d59b.0 406210212.25e32 c91@posting.goo gle.com...[color=blue]
    > Hi! Does anyone know why the onclick in the following popup menu gives
    > the error:"Val is undefined"? Does it have something to do with the
    > fact that it is called within the variable tablePop? Because it IS
    > displayed properly as part of the popup text, where it is called
    > outside the single quotation marks (see [***]). It is only in the
    > onclick that it's causing problems. Who can help me?
    >
    > function dopopup(x,y) {
    > var[/color]
    Val=event.srcEl ement.href1.sub string(0,(event .srcElement.hre f1.length-1));[color=blue]
    > var[/color]
    Field=event.src Element.href2.s ubstring(1,(eve nt.srcElement.h ref2.length-1));[color=blue]
    > var tablePop='';
    > tablePop+='<TAB LE oncontextmenu=\ "return false\";>';
    > tablePop+='<SCR IPT LANGUAGE="JavaS cript">\n';
    > tablePop+='\n<!--\n';
    > tablePop+='wind ow.onerror=null ;\n';
    > tablePop+='/-->\n';
    > tablePop+='<\/SCRIPT>\n';
    > tablePop+='<TR> <TD ONCLICK="render Data(Val,Field) ;">&nbsp;Fil ter op
    > '[***] + Field + ' is ' + Val +'</TD></TR>';
    > tablePop+='<TR> <TD>&nbsp;Filte r op ' + Field + ' is NIET '+
    > Val +'</TD></TR>';
    > tablePop+='<\/TABLE>';
    > var oPopupBody = oPopup.document .body;
    > oPopupBody.inne rHTML = tablePop;
    > oPopup.show(x, y, 140, 220, document.body);
    > }
    >
    > function renderData(filt erValue,filterF ield)
    > {
    > alert(filterVal ue);
    > alert(filterFie ld);
    > }[/color]


    Comment

    • Lee

      #3
      Re: Undefined variable error

      Sharon said:[color=blue]
      >
      >Hi! Does anyone know why the onclick in the following popup menu gives
      >the error:"Val is undefined"? Does it have something to do with the
      >fact that it is called within the variable tablePop? Because it IS
      >displayed properly as part of the popup text, where it is called
      >outside the single quotation marks (see [***]). It is only in the
      >onclick that it's causing problems. Who can help me?[/color]
      [color=blue]
      >var Val=event.srcEl ement.href1.sub string(0,(event .srcElement.hre f1.length-1));[/color]
      [color=blue]
      >tablePop+='<TR ><TD ONCLICK="render Data(Val,Field) ;">&nbsp;Fil ter op[/color]

      Val is a local variable within this function.
      It has no value anyplace else.
      The onclick handler is called someplace else.

      Val is not dereferenced (called) within the variable tablePop.
      Within that string, it's just three characters with no special
      meaning. If you want the string to contain the value of Val,
      instead of just the three characters "Val", you have to insert
      the value of the string, instead of its name:

      tablePop+='<TR> <TD ONCLICK="render Data(' + Val + ',' + Field + ');">'

      Comment

      • Mick White

        #4
        Re: Undefined variable error

        Sharon wrote:
        <snip>[color=blue]
        >
        > function dopopup(x,y) {
        > var Val=event.srcEl ement.href1.sub string(0,(event .srcElement.hre f1.length-1));
        > var Field=event.src Element.href2.s ubstring(1,(eve nt.srcElement.h ref2.length-1));[/color]

        var
        Val=event.srcEl ement.href1.sub string(0,(event .srcElement.hre f1.length-1));

        Isn't that redundant?
        Val=event.srcEl ement.href1

        Or am I missing something?

        Mick

        Comment

        • Sharon Steringa

          #5
          Re: Undefined variable error

          Yup, I have actually tried that. When I alert Val and Field right after
          I declare them or at the end, everything's fine > the values are alerted
          correctly. Just like they are displayed correctly in the popup. It's
          just within the single quotation marks, so within the other variable,
          that they aren't defined according to the browser. Thanks!


          *** Sent via Devdex http://www.devdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          • Sharon

            #6
            Re: Undefined variable error

            Lee <REM0VElbspamtr ap@cox.net> wrote in message news:<cb6r1g01e h6@drn.newsguy. com>...

            Val is not dereferenced (called) within the variable tablePop.
            Within that string, it's just three characters with no special
            meaning. If you want the string to contain the value of Val,
            instead of just the three characters "Val", you have to insert
            the value of the string, instead of its name:
            tablePop+='<TR> <TD ONCLICK="render Data(' + Val + ',' + Field + ');">'

            Thanks, I did, but now a new version of the error message appears:
            this time it says "error:[value of the variable] is undefined", so
            it's not the name of the variable, the three letters 'Val' anymore
            that aren't recognized, it's the value of the variable 'Val' that is
            undefined. So if the variable "Val" has e.g. value "Calcutta", the
            error message says "Calcutta is undefined". How can I get the variable
            passed to the function renderData? Thanks in advance!

            Comment

            • Lee

              #7
              Re: Undefined variable error

              Sharon said:[color=blue]
              >
              >Lee <REM0VElbspamtr ap@cox.net> wrote in message
              >news:<cb6r1g01 eh6@drn.newsguy .com>...
              >
              >Val is not dereferenced (called) within the variable tablePop.
              >Within that string, it's just three characters with no special
              >meaning. If you want the string to contain the value of Val,
              >instead of just the three characters "Val", you have to insert
              >the value of the string, instead of its name:
              >tablePop+='<TR ><TD ONCLICK="render Data(' + Val + ',' + Field + ');">'
              >
              >Thanks, I did, but now a new version of the error message appears:
              >this time it says "error:[value of the variable] is undefined", so
              >it's not the name of the variable, the three letters 'Val' anymore
              >that aren't recognized, it's the value of the variable 'Val' that is
              >undefined. So if the variable "Val" has e.g. value "Calcutta", the
              >error message says "Calcutta is undefined". How can I get the variable
              >passed to the function renderData? Thanks in advance![/color]

              I was thinking that the value was a number.
              Since it's a string, it needs to be quoted when used as an
              argument to renderData():

              tablePop+='<TR> <TD ONCLICK="render Data(\'' + Val + '\',\'' + Field + '\');">'

              Comment

              • Sharon Steringa

                #8
                Re: Undefined variable error

                Thanks, I had actually figured that out myself after some logical
                thinking, but it still won't work. This is what my function looks like:

                function dopopup(Val,Fie ld,x,y) {
                var popCode="";
                popCode+='<SCRI PT LANGUAGE="JavaS cript">\n';
                popCode+='\n<!--\n';
                popCode+='windo w.onerror=null; \n';
                popCode+='/-->\n';
                popCode+='<\/SCRIPT>\n';
                popCode+='<tr>< td'
                popCode+=' onClick="render Data(\'' + Val +'\');">';
                popCode+='Filte r op: '+Field+' is '+Val+'</td></tr>\n';
                popCode+='<tr>< td';
                popCode+=' onClick="render Data(\'' + Field +'\');">';
                popCode+='Filte r op: '+Field+' is '+Val+'</td></tr>\n';
                popCode+='</table>\n';
                var oPopupBody = oPopup.document .body;
                oPopupBody.inne rHTML = popCode;
                oPopup.show(x, y, 140, 220, document.body);
                }

                When I click on the first <td>, I get the error "Object expected" for
                line 12, when I click on the second one I get the same error for line
                13. I don't know if the line numbers are accurate, though. And I don't
                have a clue as to what could cause the error...Any help is greatly
                appreciated, thanks! Sharon


                *** Sent via Devdex http://www.devdex.com ***
                Don't just participate in USENET...get rewarded for it!

                Comment

                • Richard Cornford

                  #9
                  Re: Undefined variable error

                  Sharon Steringa wrote:[color=blue]
                  > Thanks, I had actually figured that out myself after some logical
                  > thinking, but it still won't work. This is what my function looks
                  > like:
                  >
                  > function dopopup(Val,Fie ld,x,y) {
                  > var popCode="";
                  > popCode+='<SCRI PT LANGUAGE="JavaS cript">\n';[/color]

                  The language attribute is deprecated in current HTML versions (and
                  absent from the HTML 4.01 strict DTD). The TYPE attribute is required
                  for valid HTML, so:-

                  popCode+='<SCRI PT TYPE="text/javascript">\n' ;
                  [color=blue]
                  > popCode+='\n<!--\n';[/color]
                  ^^^^
                  Do you understand why you are doing that?

                  In an age long past, when client-side scripting was so new that many
                  browsers in use did not know how to handle SCRIPT elements, it was used
                  as a mans of "hiding" scripts from those browsers so that they would not
                  display the SCRIPT code as contents of a page. These days even browsers
                  that cannot execute scripts know enough to ignore SCRIPT elements on
                  their own and so this is no longer needed. Unfortunately the practice
                  has propagated through the generations as a sort of folk-law practised
                  by people who have not bothered to understand why they are doing what
                  they are doing.

                  However, even if the practice was still valid there would be no point in
                  attempting to hide the contents of a SCRIPT element when that element
                  was being created with a script as a browser that could not understand
                  the SCRIPT element could never generate it.
                  [color=blue]
                  > popCode+='windo w.onerror=null; \n';[/color]

                  The default onerror handler is null or undefined on all browsers. There
                  is little point in actively setting it to that value unless it has had
                  an alternative handler assigned first.
                  [color=blue]
                  > popCode+='/-->\n';[/color]

                  The "hiding form older browsers" technique calls for the closing "HTML
                  comment" tag use in a script to follow a javascript end-of-line comment,
                  so that it will not be interpreted as a syntax error. The javascript
                  end-of-line comment is _two_ forward slashes - // - not one. The script
                  written above is - divided by, pre/post-decrement, greater than - and
                  represents a syntax error in javascript as none of those operators have
                  operands.

                  This entire SCRIPT element seems superfluous.
                  [color=blue]
                  > popCode+='<\/SCRIPT>\n';
                  > popCode+='<tr>< td'
                  > popCode+=' onClick="render Data(\'' + Val +'\');">';[/color]
                  <snip>

                  This - renderDate - function does not feature in the code you have just
                  posted but it is probably the object being referred to as "Object
                  expected". Probably it is not defined within the scope of - oPopup - and
                  would need a more qualified reference to access it. But nobody will be
                  able to tell you how to do that without seeing/knowing the full context.

                  Richard.


                  Comment

                  • Sharon Steringa

                    #10
                    Re: Undefined variable error

                    Thanks, Richard! You're right, I didn't understand why I was doing that.
                    I'm new at this Javascript-thing and I was happy to find a piece of code
                    somewhere and I'm now trying to change it so it'll work for my
                    application, which is mainly XML/XSL (that's where my skills lie). This
                    is also the reason why I don't have full control over my script and why
                    I slightly panic over my error messages...I had posted the renderData
                    function earlier in this thread, together with the other two functions I
                    use. Since it was only the dopopup-function I amended, that's the only
                    one I posted again. But I'll post the whole thing again for you:

                    function dopopup(Val,Fie ld,x,y) {
                    var popCode="";
                    popcode+='<scri pt LANGUAGE="JavaS cript">\n';
                    popcode+='\n<!--\n';
                    popCode+='windo w.onerror=null; \n';
                    popCode+='/-->\n';
                    popcode+='<\/script>\n';
                    popcode+='<tr>< td'
                    popCode+=' onClick="render Data(\'' + Val +'\');">';
                    popCode+='Filte r op: '+Field+' is '+val+'</td></tr>\n';
                    popcode+='<tr>< td';
                    popCode+=' onClick="render Data(\'' + Field +'\');">';
                    popCode+='Filte r op: '+Field+' is '+val+'</td></tr>\n';
                    popcode+='</table>\n';
                    var oPopupBody = oPopup.document .body;
                    oPopupBody.inne rHTML = popCode;
                    oPopup.show(x, y, 140, 220, document.body);
                    }

                    function renderData(filt erField){
                    alert("Joepie hij doet 't!");
                    alert(filterFie ld);
                    }

                    The two functions are in the same external .js file. I hope you or
                    anyone else will be able to help me, and/or share some more
                    javascript-knowledge because I find it very interesting and I really
                    want to get this thing to work! Thanks, Sharon



                    *** Sent via Devdex http://www.devdex.com ***
                    Don't just participate in USENET...get rewarded for it!

                    Comment

                    • Lee

                      #11
                      Re: Undefined variable error

                      Sharon Steringa said:[color=blue]
                      >
                      >Thanks, Richard! You're right, I didn't understand why I was doing that.
                      >I'm new at this Javascript-thing and I was happy to find a piece of code
                      >somewhere and I'm now trying to change it so it'll work for my
                      >application, which is mainly XML/XSL (that's where my skills lie). This
                      >is also the reason why I don't have full control over my script and why
                      >I slightly panic over my error messages...I had posted the renderData
                      >function earlier in this thread, together with the other two functions I
                      >use. Since it was only the dopopup-function I amended, that's the only
                      >one I posted again. But I'll post the whole thing again for you:
                      >
                      >function dopopup(Val,Fie ld,x,y) {
                      >var popCode="";
                      >popcode+='<scr ipt LANGUAGE="JavaS cript">\n';
                      >popcode+='\n <!--\n';
                      >popCode+='wind ow.onerror=null ;\n';
                      >popCode+='/-->\n';
                      >popcode+='<\/script>\n';
                      >popcode+='<tr> <td'
                      >popCode+=' onClick="render Data(\'' + Val +'\');">';
                      >popCode+='Filt er op: '+Field+' is '+val+'</td></tr>\n';
                      >popcode+='<tr> <td';
                      >popCode+=' onClick="render Data(\'' + Field +'\');">';
                      >popCode+='Filt er op: '+Field+' is '+val+'</td></tr>\n';
                      >popcode+='</table>\n';
                      >var oPopupBody = oPopup.document .body;
                      >oPopupBody.inn erHTML = popCode;
                      >oPopup.show( x, y, 140, 220, document.body);
                      >}[/color]

                      Javascript is case sensitive, so popcode is not the
                      same as popCode, and val is not the same as Val.

                      You're missing the opening tag for your <table>.

                      Your <script> tag should have a type attribute: type="text/javascript"

                      There's no need for the <!-- --> comments.

                      It's generally a bad idea to try to defeat the onError handler.

                      Comment

                      Working...