Support Forum Softcomplex logo
About Us  | Services  | Download  | Order  | Support  | |
| Search | Today's Posts | Stats | Board Rules | Not logged in [ Login | Register ]
go to bottom
Last active: Never

Printable Version | Subscribe | Add to Favourites   Post new thread
Author: Subject: JavaScript Dumper
tigra
Administrator

Posts: 2049
Registered: 6/17/2002
Location: US, CO
Member Is Offline
posted on 6/23/2005 at 01:23 AM Reply With Quote
JavaScript Dumper


This will return nicely formatted text dump of complex nested javascript structure. You can save the output anywhere and then restore the context feeding the text to eval function. Doesn't support functions.

Code:

function dump_var (u_var, n_depth) {

if (n_depth == null)
n_depth = 0;

var s_tabs = "\n";
for (var i = 0; i < n_depth; i++)
s_tabs += "\t";

n_depth++;
// verify type
if (typeof(u_var) == 'number')
return u_var;
else if (typeof(u_var) == 'string')
return "'" + u_var + "'";
else if (typeof(u_var) == 'boolean')
return (u_var ? 'true' : 'false');
else if (typeof(u_var) == 'undefined' || u_var == null)
return 'null';
else if (typeof(u_var) == 'function')
return 'function () {alert(\'function was not saved\')}';

// detect numeric or symbolic keys
var b_hash = false;
for (var i in u_var)
if (isNaN(Number(i))) {
b_hash = true;
break;
}

var s_output = '',
b_first = true;
if (b_hash) {
s_output += "{";
for (var i in u_var) {
s_output += (b_first ? '' : ",") + s_tabs + "\t'" + i + "': " + dump_var(u_var, n_depth);
b_first = false;
}
s_output += s_tabs + "}";
}
else {
s_output += "[";
for (var i = 0; i < u_var.length; i++) {
s_output += (b_first ? '' : ",") + s_tabs + "\t" + dump_var(u_var, n_depth);
b_first = false;
}
s_output += s_tabs + "]";
}
return s_output;
}
View User's Profile View All Posts By User U2U Member
Post new thread

Related Links:

Go To Top


Powered by XMB 1.9.1 Nexus
Developed By Aventure Media & The XMB Group © 2002-2004