Here's something that might pique your interest. I maintain an OBJECT on the client I pass to the server through a hidden text box on the form (I don't want it in a querystring -- plus the length of a querystring is limited). Obviously since you can't send an object in a hidden textbox, you must convert the object to a string first and then put it in the hidden textbox.
To get the object on the client in the first place, I create javascript code and response.write it out inside a <script></script> tag on the client. Something like response.write("function myObject(){this.Token;this.username} var token=new myObject();") I typically do this in code behind, but is can be old style server side ASP too. This puts the object initially on the client.
I also include a javascript file <script src="uneval.
js"></script> on the client that contains code that I am putting at the bottom of my post called "uneval"; What this does is decompose any object into the javascript commands (and values) that would programmatically recreate the object (and data) if executed. The output of this function is an escaped string (of javascript commands) that I stick in the hidden textbox on the form that can be posted to the server. The next page simply uses response.write("var token="+Request.FORM("hiddenTextBoxID") back to the client.
On the client, to recreate the object you simply use the eval command like var token=eval(token); The eval command executes the code generated by my uneval function and recreates the original object on the client complete with data. Note, this is designed to work with any function and should work for you like it is.
Hubman
--------------------------
//*************************
// UNEVALUATE FUNCTIONALITY
// These functions take a given jscript object and create a
// string representing the object instance in its current state,
// such that when the string is "evaluated" with the "eval"
// function, the object will be recreated. The uneval function is used
// to to "marshall" jscript objects across the client/server
// boundary. Note: unevaluated objects do not need to be 'escaped' (safe to transmit as-is)
//
var __reFunction = /function ([^\(]*)\(([^\)]*)\)[^\{]*([^$]*)/i; // function $1=name, $2=args, $3=body
var __nextObjID = 0;
var __getNextObjID = new Function('return("_o_"+(__nextObjID++));');
var __unevaledObjects; // array to track unevaluated objects
function uneval(obj,isNested)
{
var str = "";
if (!isNested) // then starting up
__unevaledObjects = new Array(); // reset tracking array of which objects unevaluated
switch (typeof(obj))
{
case 'undefined':
str = "undefined";
break;
case 'boolean':
case 'number':
str = obj.toString();
break;
case 'string':
str = 'unescape("'+escape(obj)+'")';
break;
case 'function':
if (obj.__unevalID) // manage multiple references to same function
return(obj.__unevalID);
var fnMatch = obj.toString().match(__reFunction); // split apart function definition
if (fnMatch)
{
var objID = "myToken"; //__getNextObjID(); -- cbh
obj.__unevalID = objID;
__unevaledObjects[__unevaledObjects.length] = obj; // save reference to object for later clean-up
str = "var "+objID+"=new Function(";
var args = fnMatch[2].split(",");
for (var i = 0; i < args.length; i++)
str += '"' + args[i] + '",';
str += 'unescape("'+escape(fnMatch[3])+'"));';
str += objID+";";
}
break;
case 'object':
if (obj == null)
return("null");
if (typeof(obj.getTime) == "function") // then date object
{
str = "new Date("+obj.getTime()+");";
break;
}
if (typeof(obj.substring) == "function") // then string object
{
str = 'new String(unescape("'+escape(obj.valueOf())+'"));';
break;
}
if (obj.__unevalID) // prevent endless loops (manage recursive references)
return(obj.__unevalID);
else // array or custom object
{
var property,last,ele;
var len = obj.length;
var objID = "myToken"; //__getNextObjID(); -- cbh
obj.__unevalID = objID; // add property to allow proper handling of cyclic references
__unevaledObjects[__unevaledObjects.length] = obj; // save reference to object for later clean-up
if (obj.constructor == Array) // then array (has automatic length property)
str = "var "+objID+"=new Array();";
else // object
str = "var "+objID+"=new Object();";
//str = "var "+objID+"=new Object();";
for (ele in obj)
{
if (ele == "__unevalID") // skip this (internal use only)
continue;
property = uneval(obj[ele],true);
if (property)
{
if (property.charAt(property.length-1) == ";") // then object property returned
{
last = property.lastIndexOf("_o_");
str += property.substring(0,last);
property = property.substring(last,property.length-1);
}
str += objID+"['"+ele+"']="+property+";";
}
}
str += objID+";";
}
break;
}
if (!isNested) // then doing final exit, so clean up
{
for (var xx=0; xx < __unevaledObjects.length; xx++) // remove temporary property that marked unevaluated objects and functions
delete(__unevaledObjects[xx].__unevalID);
}
return (str);
}
//
// END UNEVAL