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.srcElement.href1.substring(0,(event.srcE lement.href1.length-1));
var Field=event.srcElement.href2.substring(1,(event.sr cElement.href2.length-1));
var tablePop='';
tablePop+='<TABLE oncontextmenu=\"return false\";>';
tablePop+='<SCRIPT LANGUAGE="JavaScript">\n';
tablePop+='\n<!--\n';
tablePop+='window.onerror=null;\n';
tablePop+='/-->\n';
tablePop+='<\/SCRIPT>\n';
tablePop+='<TR><TD ONCLICK="renderData(Val,Field);"> Filter op
'[***] + Field + ' is ' + Val +'</TD></TR>';
tablePop+='<TR><TD> Filter op ' + Field + ' is NIET '+
Val +'</TD></TR>';
tablePop+='<\/TABLE>';
var oPopupBody = oPopup.document.body;
oPopupBody.innerHTML = tablePop;
oPopup.show(x, y, 140, 220, document.body);
}
function renderData(filterValue,filterField)
{
alert(filterValue);
alert(filterField);
} | | | | re: Undefined variable error
> var
Val=event.srcElement.href1.substring(0,(event.srcE lement.href1.length-1));
have you tried alerting the value of Val ? to see what it holds?
thx stu
"Sharon" <esdeees@hotmail.com> wrote in message
news:2b13d59b.0406210212.25e32c91@posting.google.c om...[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.srcElement.href1.substring(0,(event.srcE lement.href1.length-1));[color=blue]
> var[/color]
Field=event.srcElement.href2.substring(1,(event.sr cElement.href2.length-1));[color=blue]
> var tablePop='';
> tablePop+='<TABLE oncontextmenu=\"return false\";>';
> tablePop+='<SCRIPT LANGUAGE="JavaScript">\n';
> tablePop+='\n<!--\n';
> tablePop+='window.onerror=null;\n';
> tablePop+='/-->\n';
> tablePop+='<\/SCRIPT>\n';
> tablePop+='<TR><TD ONCLICK="renderData(Val,Field);"> Filter op
> '[***] + Field + ' is ' + Val +'</TD></TR>';
> tablePop+='<TR><TD> Filter op ' + Field + ' is NIET '+
> Val +'</TD></TR>';
> tablePop+='<\/TABLE>';
> var oPopupBody = oPopup.document.body;
> oPopupBody.innerHTML = tablePop;
> oPopup.show(x, y, 140, 220, document.body);
> }
>
> function renderData(filterValue,filterField)
> {
> alert(filterValue);
> alert(filterField);
> }[/color] | | | | 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.srcElement.href1.substring(0,(event.srcE lement.href1.length-1));[/color]
[color=blue]
>tablePop+='<TR><TD ONCLICK="renderData(Val,Field);"> Filter 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="renderData(' + Val + ',' + Field + ');">' | | | | re: Undefined variable error
Sharon wrote:
<snip>[color=blue]
>
> function dopopup(x,y) {
> var Val=event.srcElement.href1.substring(0,(event.srcE lement.href1.length-1));
> var Field=event.srcElement.href2.substring(1,(event.sr cElement.href2.length-1));[/color]
var
Val=event.srcElement.href1.substring(0,(event.srcE lement.href1.length-1));
Isn't that redundant?
Val=event.srcElement.href1
Or am I missing something?
Mick | | | | 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! | | | | re: Undefined variable error
Lee <REM0VElbspamtrap@cox.net> wrote in message news:<cb6r1g01eh6@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="renderData(' + 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! | | | | re: Undefined variable error
Sharon said:[color=blue]
>
>Lee <REM0VElbspamtrap@cox.net> wrote in message
>news:<cb6r1g01eh6@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="renderData(' + 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="renderData(\'' + Val + '\',\'' + Field + '\');">' | | | | 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,Field,x,y) {
var popCode="";
popCode+='<SCRIPT LANGUAGE="JavaScript">\n';
popCode+='\n<!--\n';
popCode+='window.onerror=null;\n';
popCode+='/-->\n';
popCode+='<\/SCRIPT>\n';
popCode+='<tr><td'
popCode+=' onClick="renderData(\'' + Val +'\');">';
popCode+='Filter op: '+Field+' is '+Val+'</td></tr>\n';
popCode+='<tr><td';
popCode+=' onClick="renderData(\'' + Field +'\');">';
popCode+='Filter op: '+Field+' is '+Val+'</td></tr>\n';
popCode+='</table>\n';
var oPopupBody = oPopup.document.body;
oPopupBody.innerHTML = 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! | | | | 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,Field,x,y) {
> var popCode="";
> popCode+='<SCRIPT LANGUAGE="JavaScript">\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+='<SCRIPT 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+='window.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="renderData(\'' + 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. | | | | 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,Field,x,y) {
var popCode="";
popcode+='<script LANGUAGE="JavaScript">\n';
popcode+='\n<!--\n';
popCode+='window.onerror=null;\n';
popCode+='/-->\n';
popcode+='<\/script>\n';
popcode+='<tr><td'
popCode+=' onClick="renderData(\'' + Val +'\');">';
popCode+='Filter op: '+Field+' is '+val+'</td></tr>\n';
popcode+='<tr><td';
popCode+=' onClick="renderData(\'' + Field +'\');">';
popCode+='Filter op: '+Field+' is '+val+'</td></tr>\n';
popcode+='</table>\n';
var oPopupBody = oPopup.document.body;
oPopupBody.innerHTML = popCode;
oPopup.show(x, y, 140, 220, document.body);
}
function renderData(filterField){
alert("Joepie hij doet 't!");
alert(filterField);
}
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! | | | | 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,Field,x,y) {
>var popCode="";
>popcode+='<script LANGUAGE="JavaScript">\n';
>popcode+='\n<!--\n';
>popCode+='window.onerror=null;\n';
>popCode+='/-->\n';
>popcode+='<\/script>\n';
>popcode+='<tr><td'
>popCode+=' onClick="renderData(\'' + Val +'\');">';
>popCode+='Filter op: '+Field+' is '+val+'</td></tr>\n';
>popcode+='<tr><td';
>popCode+=' onClick="renderData(\'' + Field +'\');">';
>popCode+='Filter op: '+Field+' is '+val+'</td></tr>\n';
>popcode+='</table>\n';
>var oPopupBody = oPopup.document.body;
>oPopupBody.innerHTML = 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. |  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|