473,811 Members | 3,299 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

eval() how else

given that I have a js file included which is written programatically and I
can't change it. I would like to know how to do the following using
something other than the deprecated eval().
whats in the js file
var numArrays=somet hing;
var data0 = new Array();
data0.name="nam e";
data0.data="som e data";
var data1 = new Array();
data1.name="ano ther name";
data1.data="som e more data";
etc ....
function getData(arrayNa me) {
for ( var i=0;i<numArrays :i++) {
var el=eval('data'+ i);
if (arrayName = = el.name) doSomething(el. data);
}
}

Jul 23 '05 #1
4 1923
J. J. Cale wrote on 31 aug 2004 in comp.lang.javas cript:
var el=eval('data'+ i);


el = window['data'+i]

[if a browser is used as javascript base]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #2
J. J. Cale wrote:
given that I have a js file included which is written programatically and I
can't change it. I would like to know how to do the following using
something other than the deprecated eval().
whats in the js file
var numArrays=somet hing;
var data0 = new Array();
data0.name="nam e";
data0.data="som e data";
var data1 = new Array();
data1.name="ano ther name";
data1.data="som e more data";
etc ....
function getData(arrayNa me) {
for ( var i=0;i<numArrays :i++) {
var el=eval('data'+ i); var el= window['data'+i]
// Mick
if (arrayName = = el.name) doSomething(el. data);
}
}

Jul 23 '05 #3
Lee
J. J. Cale said:

given that I have a js file included which is written programatically and I
can't change it. I would like to know how to do the following using
something other than the deprecated eval().
whats in the js file
var numArrays=somet hing;
var data0 = new Array();
data0.name="na me";
data0.data="so me data";
var data1 = new Array();
data1.name="an other name";
data1.data="so me more data";
etc ....
function getData(arrayNa me) {
for ( var i=0;i<numArrays :i++) {
var el=eval('data'+ i);
if (arrayName = = el.name) doSomething(el. data);
}
}


I don't know that eval() is deprecated.
It's just usually not the best way to do something.

Assuming that this is in a web page, you can replace:
eval('data'+i)
with:
window['data'+i]

The fact that the variables are defined in a .js file
isn't really significant. The contents of that file
are evaluated in the context of the current window.

Jul 23 '05 #4
J. J. Cale wrote:
given that I have a js file included which is written programatically and
I can't change it.
Why?
I would like to know how to do the following using something other than
the deprecated eval().
whats in the js file
var numArrays=somet hing;
var data0 = new Array();
data0.name="nam e";
data0.data="som e data";
var data1 = new Array();
data1.name="ano ther name";
data1.data="som e more data";
etc ....
function getData(arrayNa me) {
for ( var i=0;i<numArrays :i++) {
var el=eval('data'+ i);
if (arrayName = = el.name) doSomething(el. data);
}
}


var _global = this;

function getData(arrayNa me)
{
for (var i = 0; i < numArrays; i++)
{
var el = _global['data' + i];
if (arrayName == el.name)
{
doSomething(el. data);
}
}
}

But:
Why do you not use an Array object in the first place?
Why do data0, data1 aso. refer to an Array object anyway?

var data = [
{name: "name",
data: "some data"},
{name: "another name",
data: "some more data"}];

Or define and use a prototype:

function Data(sName, d)
{
this.name = sName;
this.data = d;
}

var data = [
new Data("name", "some data"),
new Data("another name", "some more data")];
PointedEars

P.S.
Please use empty lines in your postings for
paragraphs to make them easier legible.
--
Our only hope lies in the mind of Kiki
Jul 23 '05 #5

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

Similar topics

9
8471
by: HikksNotAtHome | last post by:
This is a very simplified example of an Intranet application. But, is there an easier (or more efficient) way of getting the decimal equivalent of a fraction? The actual function gets the select values, this one is a simplified version where its passed. function checkIt(selVal){ valueInDec1 = eval(selVal); //do some calculations here with valueInDec1 }
7
4113
by: Reply Via Newsgroup | last post by:
This might sound sad... someone requesting a disertation on the 'eval' statement... but... I've been reading someone else's post - they had a huge calander like script and a handful of folk cursed the script and special attention was thrown at the fact the script used eval alot. I don't use eval alot in my scripts - but I do use it - and since I always out to learn more / improve my javascript skills, I'm curious why something I thought...
2
3166
by: Billy | last post by:
I want to make a calendar and add a hyperlink to the days inside the calendar. The source code is below: <body> <SCRIPT LANGUAGE="JavaScript"> var now = new Date();
12
3463
by: knocte | last post by:
Hello. I have always thought that the eval() function was very flexible and useful. If I use it, I can define functions at runtime!! However, I have found a case where eval() does not work properly. It works, for example, when invoking functions (alert('hello')), but not for defining functions. The case occurs when retrieving the javascript code with
8
13066
by: werner | last post by:
Hi! I don't want to use eval() in order to parse a user-supplied formula. What alternatives do I have? PHP has no standard functionality for tokenizing or parsing expressions in this regard. Here is a simple example: The user supplies the following formula in string format, "a = (6+10)/4", and the script needs to find out what the value of 'a' is.
3
13735
by: idletask | last post by:
I have an SQLDataSource control, which is bound to an ASP.net 2.0 GridView control. It outputs a list of documents that a user can check in or check out of the database. In my gridview, I have a column called "checked out" which is bound to a column in my database, called "checkedout". A zero means no, and a one means yes (that the document is checked out). Showing a 0 or 1 is ugly and I want to use No or Yes. I can't figure out...
15
3673
by: manstey | last post by:
Hi, I have a text file called a.txt: # comments I read it using this:
5
1969
by: Smiley | last post by:
I'm fooling around with using Eval and trying to manipulate a few things. I ran into a couple of weird results. First of all, in one place I used the following code: $filestring = str_replace("<?", "\n<?\n", $filestring); $filestring = str_replace("?>", "\n?>\n", $filestring); Not a huge thing, just making things easier to read for me. But doing this gives me an error, even when I comment those lines out. I have to remove them...
5
6638
by: wendallsan | last post by:
Hi all, I'm running into a situation where it seems that JS stops executing as soon as I call an eval in my script. I have an Ajax.Request call to a PHP page that builds a JS object and returns it as the responseText. I want to attach that object to the document (or anywhere else that would allow me to access it later), and to do that, I THINK I need to eval it because it's just a string otherwise. My problem is as soon as I execute a...
10
494
by: Gordon | last post by:
I have a script that creates new objects based on the value of a form field. Basically, the code looks like this. eval ('new ' + objType.value + '(val1, val2, val3'); objType is a select with the different types of objects you can create as values. I really don't like using eval, and it's causing problems, like if I do something like the following:
0
9734
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
9607
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10137
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...
1
7673
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
6895
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
5561
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
5700
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3874
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3026
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.