Undefined variable error

S

Sharon

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.srcElement.href1.length-1));
var Field=event.srcElement.href2.substring(1,(event.srcElement.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);">&nbsp;Filter op
'[***] + Field + ' is ' + Val +'</TD></TR>';
tablePop+='<TR><TD>&nbsp;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);
}
 
S

Stuart Palmer

var
Val=event.srcElement.href1.substring(0,(event.srcElement.href1.length-1));

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

thx stu

Sharon said:
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.srcElement.href1.length-1));
Field=event.srcElement.href2.substring(1,(event.srcElement.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);">&nbsp;Filter op
'[***] + Field + ' is ' + Val +'</TD></TR>';
tablePop+='<TR><TD>&nbsp;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);
}
 
L

Lee

Sharon said:
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?
var Val=event.srcElement.href1.substring(0,(event.srcElement.href1.length-1));
tablePop+='<TR><TD ONCLICK="renderData(Val,Field);">&nbsp;Filter op

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 + ');">'
 
M

Mick White

Sharon wrote:
function dopopup(x,y) {
var Val=event.srcElement.href1.substring(0,(event.srcElement.href1.length-1));
var Field=event.srcElement.href2.substring(1,(event.srcElement.href2.length-1));

var
Val=event.srcElement.href1.substring(0,(event.srcElement.href1.length-1));

Isn't that redundant?
Val=event.srcElement.href1

Or am I missing something?

Mick
 
S

Sharon Steringa

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

Sharon

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

Lee

Sharon said:
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!

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 + '\');">'
 
S

Sharon Steringa

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

Richard Cornford

Sharon said:
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';

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+=' said:
popCode+='\n<!--\n';
^^^^
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.
popCode+='window.onerror=null;\n';

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.
popCode+='/-->\n';

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.
popCode+='<\/SCRIPT>\n';
popCode+='<tr><td'
popCode+=' onClick="renderData(\'' + Val +'\');">';
<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.
 
S

Sharon Steringa

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

Lee

Sharon Steringa said:
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);
}

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top