toLiteral.js
 

toLiteral

Description:
This library enables you to serialize javascript variables to a string, which can be evaluated. Very convenient when you need the data server-side, or while debugging your client-side code. The code is optimized for performance and compatibility.

Current version: v1.3 (2006-04-13). See code for details
Download: code, view-source
Tested win: IE5/6, Firefox 1.0.7/1.5; Opera7.51/9.00
Tested mac: IE5.2.3, Firefox 1.0.6/1.5; Safari 1.3.2/2.0.2/Nightly2006-04-13; Opera 7.54 + 8.52
Tested server: IIS5.0/ASP

Usage

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

Test toLiteral

Known issues

  • Doesn't work with Opera 6.02/Mac.
  • No pretty-print/formatting is implemented.

History

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. This is a rewrite of my toSource library. It's a rewrite, to boost performance. The library sports a method and a function interface. When you serialize variables