This library is deprecated. Please use the rewrite toLiteral.

Within the Netscape-implementation, every object implements the toSource method, which converts the object into an javascript-literal. This library provides the function toSource to convert any variable (number, string, boolean, object, array, date-object, etcetera) into a literal.

ToDo: Look into differences with Mozilla.
Download: code, view-source, rewrite, still in beta (toLiteral),
Targetted: all
Tested on: win32: ie5-ie6, mac: ie5


This library comes in handy for communication between server- and client-side ASP (assuming you use javascript serverside).

/*ASP with Javascript*/
var str='I\'m creating a string\nwith escape codes.';
Response.Write('var str='+toSource(str)+';\n');

You can use this lib as a light-weight, low-end browser replacement for XML. With javascript, you create a datastructure of arrays/objects that contain strings and nubmers. This structure can be accessed programmatically. For example, implement a client-side only photo-album. You can use this library to scan all pictures on width and height and write those values into the javascript structure.

Live Demo

This example loads the javascript-structure, which I use to build up the menu on the left. Press load to load the URL pointing to javascript code, and convert the variable named in the 2nd textbox into a literal.


After I got the idea, and programmed the biggest part, I found out, Netscape already has native support for this functionality. Netscape Javascript has the method toSource(), but ECMA (and therefor IE) has not. The NS-implementation can't be simulated in IE, because a number is not cast to an number-object automatically