473,804 Members | 3,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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('fr mCntrl_tbl_' + divID) == 'object' //this
fails when the object isn't created yet

Any Ideas Would GREATLY be Appreciated!!

Vmusic

==============c ode 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.create Element('table' );
eval('var frmCntrl_tbl_' + divID + ' =
document.create Element(\'table \');');
alert('There is a variable frmCntrl_tbl_' + divID + 'of type: ' +
typeof(eval('va r frmCntrl_tbl_' + divID)));

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

Mar 26 '06 #1
6 3126
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.getEle mentById('frmCn trl_tbl_' + divID) ){
theTable = document.create Element('table' );
theTable.id = 'frmCntrl_tbl_' + divID;
// and so on...
}
// theTable is a reference to the table


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

Any Ideas Would GREATLY be Appreciated!!

Vmusic

==============c ode below========== =============

//determine if a variable for this div already exists
if(typeof(eval( 'frmCntrl_tbl_' + divID) == 'object')) //if the object
[...]
//var frmCntrl_tbody = document.create Element('tbody' );
eval('var frmCntrl_tbody_ ' + divID + ' =
document.create Element(\'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.insert Row(-1);

// Add a cell to the row
var aCell = aRow.insertCell (-1);
aCell.appendChi ld(document.cre ateTextNode('Ne w 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('fr mCntrl_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#inferen ce>
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.getEle mentById('frmCn trl_tbl_' + divID) ){

That is missing a closing bracket, it should be:

if ( !(theTable = document.getEle mentById('frmCn trl_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....an y
ideas....
I want a:
var someFixedPart + someDynamicPart = document.create Element('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.c om, 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....an y
ideas....

I want a:
var someFixedPart + someDynamicPart = document.create Element('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'+count er]=dynamicContent 1+'staticConten t'+dynamicConte nt2;
--
Randy
comp.lang.javas cript 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....an y
ideas....
I want a:
var someFixedPart + someDynamicPart = document.create Element('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.getEl ementById( bothParts )){

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

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

Which can be abbreviated to:

var theTable;
if ( !(theTable = document.getEle mentById( bothParts )) ){
theTable = document.create Element('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.create Element('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
4305
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 code everytime I found more than one method to perform a single task. I timed each method to find which would complete faster. I thought I'd share my most recent results which (I believe) should help those write their programs to be more...
5
3388
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' is not defined" Please, could you tell me what for function exist to test the variable with Python?
7
2567
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 style and false if not. Have you any idea? Thank in advance an dbest regards. Pietro.
1
3965
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 the <div>. Doing: blah = getElementById("thumbnails"); Generates an error.... I was hoping it would just return false or something... Is there a way of doing this? P.
14
7709
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 alternatives? Additionally, might there be cross-platform alternatives, say in a library like Boost, or something else? -Matt
12
1814
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
1571
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 of class A is unknown) I need a way to
9
4126
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 __LINE__): #ifdef __LINE__ (code that uses __LINE__)
2
10051
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 is used to access and read the value stored in it C.A variable is allocated or deallocated in memory during runtime D.A variable can be initialized at the time of its creation or later 2. The.……types feature facilitates the definition of classes...
0
9704
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10562
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10070
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9132
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6845
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5508
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.