Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Javascript] dynamically creating variables using eval() SOLVED


  • From: javascript@xxxxxxxxxx (Lau)
  • Subject: [Javascript] dynamically creating variables using eval() SOLVED
  • Date: Thu, 23 Aug 2001 19:12:37 +1000

This is a multi-part message in MIME format.

------=_NextPart_000_0028_01C12C07.9297CE20
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

innerHTMLFor those who are interested I solved the problem by first =
creating an object "varlist" and then dynamically adding properties to =
the object.

<html><head>
<SCRIPT LANGUAGE=3D"JavaScript1.2">
<!--
var varlist =3D new Object(Object);
// -->
</SCRIPT>
</head><body>
<A HREF=3D# =
onclick=3D'Javascript:eval("varlist.prototype.newvar=3D11");'>create =
variable "newvar"</A><BR>
<A HREF=3D# =
onclick=3D'Javascript:alert("varlist.newvar=3D"+varlist.newvar);'>view  =
"newvar"</A>
</body></html>


regards
Lau
  ----- Original Message -----=20
  From: Lau=20
  To: javascript@xxxxxxxxxx=20
  Sent: Thursday, August 23, 2001 6:06 PM
  Subject: [Javascript] dynamically creating variables using eval()


  Hi all,

  How can I dynamically create a new javascript variable that will be =
available to subsequent scripts.
  I can use eval("var newvar;newvar=3D33;") to create the variable, but =
the new variable only seems to exist within the scope of the script =
block that it was created in. Below is a page that demonstrates what I =
mean... the first alert works, the second one says that newvar is =
undefined.
  =20
  <html><head></head><body>
  <A HREF=3D# onclick=3D'Javascript:eval("var =
newvar;newvar=3D33;");alert(newvar);'>create variable "newvar"</A><BR>
  <A HREF=3D# onclick=3D'Javascript:alert("newvar=3D"+newvar);'>view  =
"newvar"</A>
  </body></html>

  Thanks in advance
  Lau

------=_NextPart_000_0028_01C12C07.9297CE20
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>innerHTML</TITLE>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2614.3500" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>For those who are interested I solved =
the problem=20
by first creating an object "varlist" and then dynamically adding =
properties to=20
the object.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&lt;html&gt;&lt;head&gt;<BR>&lt;SCRIPT=20
LANGUAGE=3D"JavaScript1.2"&gt;<BR>&lt;!--<BR>var varlist =3D new=20
Object(Object);<BR>//=20
--&gt;<BR>&lt;/SCRIPT&gt;<BR>&lt;/head&gt;&lt;body&gt;<BR>&lt;A HREF=3D# =

onclick=3D'Javascript:eval("varlist.prototype.newvar=3D11");'&gt;create =
variable=20
"newvar"&lt;/A&gt;&lt;BR&gt;<BR>&lt;A HREF=3D#=20
onclick=3D'Javascript:alert("varlist.newvar=3D"+varlist.newvar);'&gt;view=
&nbsp;=20
"newvar"&lt;/A&gt;<BR>&lt;/body&gt;&lt;/html&gt;</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>regards</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Lau</FONT></DIV>
<BLOCKQUOTE=20
style=3D"BORDER-LEFT: #000000 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: =
0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px">
  <DIV style=3D"FONT: 10pt arial">----- Original Message ----- </DIV>
  <DIV=20
  style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: =
black"><B>From:</B>=20
  <A href=3D"mailto:lau@xxxxxxxxxxxxxxxx"; =
title=3Dlau@xxxxxxxxxxxxxxxx>Lau</A>=20
</DIV>
  <DIV style=3D"FONT: 10pt arial"><B>To:</B> <A=20
  href=3D"mailto:javascript@xxxxxxxxxx"=20
  title=3Djavascript@xxxxxxxxxx>javascript@xxxxxxxxxx</A> </DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Sent:</B> Thursday, August 23, 2001 =
6:06=20
  PM</DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Subject:</B> [Javascript] =
dynamically=20
  creating variables using eval()</DIV>
  <DIV><BR></DIV>
  <DIV><FONT face=3DArial size=3D2>Hi all,</FONT></DIV>
  <DIV>&nbsp;</DIV>
  <DIV><FONT face=3DArial size=3D2>How can I dynamically create a new =
javascript=20
  variable that will be available to subsequent scripts.</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2>I can use eval("var =
newvar;newvar=3D33;") to create=20
  the variable, but the new variable only seems to exist within the =
scope of the=20
  script block that it was created in. Below is a page that demonstrates =
what I=20
  mean... the first alert works, the second one says that newvar is=20
  undefined.</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
  <DIV><FONT face=3DArial=20
  size=3D2>&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;<BR>&lt;A =
HREF=3D#=20
  onclick=3D'Javascript:eval("var =
newvar;newvar=3D33;");alert(newvar);'&gt;create=20
  variable "newvar"&lt;/A&gt;&lt;BR&gt;<BR>&lt;A HREF=3D#=20
  onclick=3D'Javascript:alert("newvar=3D"+newvar);'&gt;view&nbsp;=20
  "newvar"&lt;/A&gt;<BR>&lt;/body&gt;&lt;/html&gt;</FONT></DIV>
  <DIV>&nbsp;</DIV>
  <DIV><FONT face=3DArial size=3D2>Thanks in advance</FONT></DIV>
  <DIV><FONT face=3DArial =
size=3D2>Lau</FONT></DIV></BLOCKQUOTE></BODY></HTML>

------=_NextPart_000_0028_01C12C07.9297CE20--