Forum Moderators: open
I Have a question about global variables.
I have diffrent scripts wich need to interact with eachoter.
For example, i have a few forms and i want to enum the elements of that form and put them in a global var so i can use those everywhere on my site whenever there is a form created.
Ill give a simple example.
----
var _global;
function setGlobals() {
//This will be an loop ofcourse normaly
//And all _global.someElement will have diff names
_global.someElement = document.getElementById('blah');
}
function showGlobals() {
//Just to show what is in it.
//The var _global must also be accessable from other scripts
alert(_global);
}
----
Now is this possible someway?
That i have a _global var and set it like that?
_global.myString = 'Test';
and when i enum _global or something i can get the values?
Thx in advanced.
BlackDex
What i thought is something like _global.*
Where * would a unique HTML object for instanse.
So when i do something like _global.elements or something it will show all the elements that _global. has.
And not that i have to type every .* by hand (where * is object1, object2 etc....).
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset:utf-8">
<title>Object Oriented Properties</title>
<script type="text/javascript">function Properties()
{
// An array to hold properties and values
this.props = new Array();
}// A function to set a property
Properties.prototype.setProperty = function(prop,val)
{
// Make sure there's no property by this name first
for( var i = 0; i < this.props.length; i++ )
{
if( this.props[i][0] == prop )
{
this.props[i][1] = val;
return;
}
}
this.props[this.props.length] = new Array(prop,val);
}// A function to get a named property
Properties.prototype.getProperty = function(prop)
{
for( var i = 0; i < this.props.length; i++ )
{
if( this.props[i][0] == prop )
{
return this.props[i][1];
}
}
return "";
}// A function to get array of all properties
Properties.prototype.getProperties = function()
{
return this.props;
}Properties.prototype.toString = function()
{
var result = "";
for( var i = 0; i < this.props.length; i++ )
{
result += "<div>";
result += ( this.props[i][0] + "=" + this.props[i][1] );
result += "<\/div>";
}
return result;
}// Example:
var _globals = new Properties();
_globals.setProperty("foo","Hello");
_globals.setProperty("bar","World");
_globals.setProperty("abc","123");
_globals.setProperty("xyz","456");
</script></head>
<body><script type="text/javascript">
document.write(_globals.toString());
</script>
<p>
The value of global "foo" is:
<script type="text/javascript">
document.write(_globals.getProperty("foo"));
</script>
</p>
<p>
Change the value of "foo" to "Goodbye"...
<script type="text/javascript">
_globals.setProperty("foo","Goodbye");
</script>
</p>
<p>
The new value of global "foo" is:
<script type="text/javascript">
document.write(_globals.getProperty("foo"));
</script>
</p>
<p>
And the globals now looks like this:
<script type="text/javascript">
document.write(_globals.toString());
</script>
</p>
</div>
</body>
</html>
Now i only have one little prob left.
I Have something like this.
-------
function setGlobal(formname) {
var objForm = document.getElementById(formname);
var x = objForm.obj.elements;
for (var i=0; i < x.length; i++) {
objName = x[i].id;
_global.String('obj'+objName) = new getObj(x[i].id);
}
}
-------
Now i can't get the _global. part working, so that after the . the name would be something like objTextbox or objRadiobutton etc...
I get an error about type mismatch.
Now that is something i can understand..
But is there a way to get this working?
Thx in advanced.
_global.String('obj'+objName) = new getObj(x[i].id);
}
}
-------Now i can't get the _global. part working, so that after the . the name would be something like objTextbox or objRadiobutton etc...
Try this:
eval("_global.obj" + objName + " = new getObj(" + x[i].id + ")");
EDIT: or maybe:
eval("_global.obj" + objName + " = new getObj('" + x[i].id + "')");
---
function setGlobal() {
var objForm = document.getElementById('form1');
var x = objForm.elements;
for (var i=0; i < x.length; i++) {
objName = "obj" + x[i].id;
eval("_global."+ objName + " = document.getElementById('" + x[i].id + "')");
}
}
---
This now works like a charm :D.....
I Whas already checking with eval, but hadn't figuerd it out yet.
Again thx for the help :D.
Kind regards,
BlackDex.