String tricks

by Alon Rotem

JavaScript

//--------------------------------------------------------
//encoding/decoding a url with escape() / unescape() - deprecated

alert("escape: " + escape("This is < + > fun!"));
alert("unescape: " + unescape("This%20is%20%3C%20+%20%3E%20fun%21"));
=

//encoding/decoding a url with encodeURI() / decodeURI() - deprecated
/*
alert("encodeURI: " + encodeURI("This is < + > fun!"));
alert("decodeURI: " + decodeURI("This%20is%20%3C%20+%20%3E%20fun%21"));
*/

//the built-in trimmer
//alert("[" + "      gggg       ".trim() + "]");

//Escaping html with jquery
var someHtmlString = "<" + "script " + ">alert('hi!');<" + " /script " + ">";
var escaped = $('<div/>').text(someHtmlString).html();
//alert(escaped);

//stringify a JSON object
var person = { 
    firstname: "Alon",
    lastname: "Rotem",
    company: "Telerik",
    weekendDays: ["Saturday", "Sunday"]
};
//alert(JSON.stringify(person, null, 10));

var objectString = '{"firstname":"Alon","lastname":"Rotem","company":"KPMG"}';
var converted = JSON.parse(objectString);
//alert("Converted to object: " + converted);
//alert("object property: " + converted.firstname);