Forum Moderators: open

Message Too Old, No Replies

Global variables

Howto?

         

BlackDex

9:02 am on Dec 14, 2005 (gmt 0)

10+ Year Member



Hello there,

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

kaled

12:48 pm on Dec 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Global vars in the same document are shared irrespective of how many unique scripts are loaded into the document.

Kaled.

BlackDex

5:35 pm on Dec 14, 2005 (gmt 0)

10+ Year Member



Well.. that is nice :).
But is there some way to enum those variables?

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....).

Fotiman

5:46 pm on Dec 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can do that using the for/in method on an object. For example:

<script type="text/javascript">
var _global = new Object();
_global.property1 = "Hello";
_global.something = "World";
_global.foo = 123;

for( p in _global )
{
alert(p + "=" + eval("_global." + p));
}
</script>

Fotiman

6:24 pm on Dec 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Also, you could improve upon that by making it more object oriented. For example, you want an object that takes a bunch of property names and values, and that you can get the value of individual properties or enumerate all properties.


<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>

BlackDex

6:45 pm on Dec 14, 2005 (gmt 0)

10+ Year Member



Ahhh.. thx.. That works verywell..

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.

Fotiman

6:51 pm on Dec 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




_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 + "')");

BlackDex

7:01 pm on Dec 14, 2005 (gmt 0)

10+ Year Member



Thank you very very very much :D :D

---
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.