Test for existence of dynamic variable

V

Vmusic

Hi,
I am using Javascript to add rows to tables, etc. in a function I am
calling. I pass the function the ID of the div, and what I want in the
rows, and it will add rows to a table in the div.

The problem is I need to test for the existence of the table - and if
the variable or object doesn't exist already my code errors -
PLEASE REMEMBER - I don't know the name of the variable or object I am
testing the existance for - it is created dynamically based on the
divID. So when I test for this object or variable the test has to be
for a dynamically created object -

I tried typeof(eval('frmCntrl_tbl_' + divID) == 'object' //this
fails when the object isn't created yet

Any Ideas Would GREATLY be Appreciated!!

Vmusic

==============code below=======================

//determine if a variable for this div already exists
if(typeof(eval('frmCntrl_tbl_' + divID) == 'object')) //if the object
does not exist this ERRORS
{
alert('A table variable with the divID ' + divID + ' already exists');
}
else
{
alert('First call the to create the table');
//create a table to hold the options or features for this control
//var frmCntrl_tbl = document.createElement('table');
eval('var frmCntrl_tbl_' + divID + ' =
document.createElement(\'table\');');
alert('There is a variable frmCntrl_tbl_' + divID + 'of type: ' +
typeof(eval('var frmCntrl_tbl_' + divID)));

//var frmCntrl_tbody = document.createElement('tbody');
eval('var frmCntrl_tbody_' + divID + ' =
document.createElement(\'tbody\');');
}
 
R

RobG

Vmusic said on 27/03/2006 6:02 AM AEST:
Hi,
I am using Javascript to add rows to tables, etc. in a function I am
calling. I pass the function the ID of the div, and what I want in the
rows, and it will add rows to a table in the div.

The problem is I need to test for the existence of the table - and if
the variable or object doesn't exist already my code errors -
PLEASE REMEMBER - I don't know the name of the variable or object I am
testing the existance for - it is created dynamically based on the
divID. So when I test for this object or variable the test has to be
for a dynamically created object -

To test for the existence of the table:

var theTable;
if ( !(theTable = document.getElementById('frmCntrl_tbl_' + divID) ){
theTable = document.createElement('table');
theTable.id = 'frmCntrl_tbl_' + divID;
// and so on...
}
// theTable is a reference to the table

I tried typeof(eval('frmCntrl_tbl_' + divID) == 'object' //this
fails when the object isn't created yet

Any Ideas Would GREATLY be Appreciated!!

Vmusic

==============code below=======================

//determine if a variable for this div already exists
if(typeof(eval('frmCntrl_tbl_' + divID) == 'object')) //if the object
[...]

//var frmCntrl_tbody = document.createElement('tbody');
eval('var frmCntrl_tbody_' + divID + ' =
document.createElement(\'tbody\');');
}

Rather than using create/add for tbody, rows & cells, consider using
insertRow and insertCell. Then you can add rows directly to the table
(no issues with tbody in IE), and the rows/cells are added in one line
rather than two separate lines:

// Add a row:
var aRow = theTable.insertRow(-1);

// Add a cell to the row
var aCell = aRow.insertCell(-1);
aCell.appendChild(document.createTextNode('New cell'));


They are DOM 1 methods and so well supported, though they are a little
slow in IE if you add more than a hundred or so at once.

<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-39872903>
<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-68927016>
 
T

Thomas 'PointedEars' Lahn

Vmusic said:
The problem is I need to test for the existence of the table - and if
the variable or object doesn't exist already my code errors -
PLEASE REMEMBER - I don't know the name of the variable or object I am

Please do not SHOUT.
testing the existance for - it is created dynamically based on the
divID. So when I test for this object or variable the test has to be
for a dynamically created object -

I tried typeof(eval('frmCntrl_tbl_' + divID) == 'object' //this
fails when the object isn't created yet

Drop the evil[tm] eval() nonsense, and your problems will most certainly
go away.
Any Ideas Would GREATLY be Appreciated!!

Search the archives about "eval", and read
<URL:http://pointedears.de/scripts/test/whatami#inference>


PointedEars
 
R

RobG

RobG said on 27/03/2006 9:11 AM AEST:

[...]
To test for the existence of the table:

var theTable;
if ( !(theTable = document.getElementById('frmCntrl_tbl_' + divID) ){


That is missing a closing bracket, it should be:

if ( !(theTable = document.getElementById('frmCntrl_tbl_' + divID)) ){


[...]
 
V

Vmusic

OK.... Look - I'll give up my eval() - **BUT ONLY***
if you show me how to do I create a dynamic variable
Your reference didn't show how to create TRULY dynamic variables....any
ideas....


I want a:
var someFixedPart + someDynamicPart = document.createElement('table');

where the someDynamicPart is a variable passed in and is different each
time

????????????????

Vmusic
 
R

Randy Webb

Vmusic said the following on 3/26/2006 10:37 PM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.
OK.... Look - I'll give up my eval() - **BUT ONLY***
if you show me how to do I create a dynamic variable
Your reference didn't show how to create TRULY dynamic variables....any
ideas....

I want a:
var someFixedPart + someDynamicPart = document.createElement('table');
where the someDynamicPart is a variable passed in and is different each
time

How dynamic do you want it?

All global variables, in browsers, are properties of the window object.

eval code:

eval('var someVar' + counter + ' = ' + dynamicContent1 + 'staticContent'
+ dynamicContent2);

Non-eval version:

window['someVar'+counter]=dynamicContent1+'staticContent'+dynamicContent2;
 
R

RobG

Vmusic said on 27/03/2006 1:37 PM AEST:
OK.... Look - I'll give up my eval() - **BUT ONLY***
if you show me how to do I create a dynamic variable
Your reference didn't show how to create TRULY dynamic variables....any
ideas....


I want a:
var someFixedPart + someDynamicPart = document.createElement('table');

where the someDynamicPart is a variable passed in and is different each
time

Going back to your original post, you wanted:

"The problem is I need to test for the existence of the table - and
if the variable or object doesn't exist already my code errors"


You've been shown how to deal with that. To reiterate:

function blah( dynamicPart )
{
var bothParts = 'foo_' + dynamicPart;
var theTable;

if (document.getElementById( bothParts )){

// An element exists with that ID
theTable = document.getElementById( bothParts );
} else {

// An element with that ID doesn't exist, so make one
theTable = document.createElement('table');
theTable.id = bothParts;
}
}

Which can be abbreviated to:

var theTable;
if ( !(theTable = document.getElementById( bothParts )) ){
theTable = document.createElement('table');
theTable.id = bothParts;
// Add the table to the document
}
// Now do stuff with theTable.


If, when you create the table, you want to keep a reference to it, then
use an object:

var storedRefs = {};

function blah( dynamicPart )
{
var bothParts = 'foo_' + dynamicPart;
var theTable;

if (bothParts in storedRefs){

// Have a stored reference so use it
theTable = storedRefs[bothParts];
} else {

// No stored reference, create table & store reference
theTable = document.createElement('table');
theTable.id = bothParts;
storedRefs[bothParts] = theTable;
}
}
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top