473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Test for existence of dynamic variable

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\');');
}

Mar 26 '06 #1
6 3095
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>
--
Rob
Mar 26 '06 #2
Vmusic wrote:
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
Mar 26 '06 #3
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)) ){
[...]

--
Rob
Mar 27 '06 #4
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

Mar 27 '06 #5
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.
<URL: http://www.safalra.com/special/googlegroupsreply/ >
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;
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 27 '06 #6
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;
}
}

--
Rob
Mar 27 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Randell D. | last post by:
Folks, Ever since reading an interesting article in Linux Format on PHP whereby suggested code writing was made that could enhance performance on a server, I've started testing various bits of...
5
by: Thierry S. | last post by:
Hello. I would to test the existence of a variable before to use it (like isset($myVar) in PHP). I try using "if myVar: ", but there is the error meesage (naturally): "NameError: name 'myVar'...
7
by: Pietro | last post by:
Hi at all, I am looking for a mean to test if a function work with a certain Browser or not. I'ld like to make a funcrion that return true if the browse is compatible with a certain funcrion or...
1
by: The Plankmeister | last post by:
Is it possible to check for the existence of an element? I have a dynamic page which may or may not have a <div> holding a bunch of thumbnails, and I want a function to check for the existence of...
14
by: Matt | last post by:
Hello, I see other references in this newsgroup saying that the only standard C++ way to test for file existence is some variant of my code below; can someone please confirm...or offer...
12
by: DC Gringo | last post by:
How do I test for existence of a file in the file system: If FileExists(myVariable & ".pdf") = True pnlMyPanel.Visible = True End If -- _____ DC G
9
by: wildernesscat | last post by:
Hello there, I'm looking for a method to test, whether an object has a certain property. Consider the following snippet: class A { var $aaa; } $var = new A; (Assuming that the structure...
9
by: dave_140390 | last post by:
Hi, Is there a clean way to determine at compile-time whether your compiler supports the __func__ feature? I mean, something similar to the following (which tests for the existence of macro...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.