1.JSMin
2.Dojo ShrinkSafe
3.YUI Compressor
1.JsUnit
2.YUI Test
3.DOH
4.qUnit
1.JsDoc Toolkit
2.YUI Doc
3.AjaxDoc
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>Shopping list</title> <style type="text/css"> p { color: yellow; font-family: "arial", sans-serif; font-size: 1.2em; } body { color: white; background-color: black; } #purchases { border: 1px solid white; background-color: #333; color: #ccc; padding: 1em; } #purchases li { font-weight: bold; } </style> </head> <body> <h1>What to buy</h1> <p title="a gentle reminder">Don't forget to buy this stuff.</p> <p>This is just a test</p> <ul id="purchases"> <li>A tin of beans</li> <li>Cheese</li> <li>Milk</li> </ul> <script type="text/JavaScript"> var paras = document.getElementsByTagName("p"); for (var i=0; i< paras.length; i++) { var title_text = paras[i].getAttribute("title"); if (title_text != null) { alert(title_text); } } </script> </body> </html>
1.Number() 2.parseInt(); 3.parseFloat();
var num1 = Number("Hello world!"); //NaN var num2 = Number(""); //0 var num3 = Number("000011"); //11 var num4 = Number(true); //1 var num1 = parseInt("1234blue"); //1234 var num2 = parseInt(""); //NaN var num3 = parseInt("0xA"); //10 - hexadecimal var num4 = parseInt(22.5); //22 var num5 = parseInt("70"); //70 - decimal var num6 = parseInt("0xf"); //15 ?hexadecimal var num1 = parseInt("AF", 16); //175 var num2 = parseInt("AF"); //NaN var num1 = parseInt("10", 2); //2 ?parsed as binary var num2 = parseInt("10", 8); //8 ?parsed as octal var num3 = parseInt("10", 10); //10 ?parsed as decimal var num4 = parseInt("10", 16); //16 ?parsed as var num1 = parseFloat("1234blue"); //1234 - integer var num2 = parseFloat("0xA"); //0 var num3 = parseFloat("22.5"); //22.5 var num4 = parseFloat("22.34.5"); //22.34 var num5 = parseFloat("0908.5"); //908.5 var num6 = parseFloat("3.125e7"); //31250000
var age = 11; var ageAsString = age.toString(); //the string "11" var found = true; var foundAsString = found.toString(); //the string "true" alert(ageAsString); alert(typeof ageAsString); alert(foundAsString); alert(typeof foundAsString); var num = 10; alert(num.toString()); //"10" alert(num.toString(2)); //"1010" alert(num.toString(8)); //"12" alert(num.toString(10)); //"10" alert(num.toString(16)); //"a" var value1 = 10; var value2 = true; var value3 = null; var value4; alert(String(value1)); //"10" alert(String(value2)); //"true" alert(String(value3)); //"null" alert(String(value4)); //"undefined"
var person = new Object(); person.name = "Nicholas"; person.age = 29; var person = { name : "Nicholas", age : 29 };
var colors = new Array(3); //create an array with three items var names = new Array("Greg"); //create an array with one item, the string "Greg" alert(colors.length); alert(names.length); var colors = ["red", "blue", "green"]; //creates an array with three strings var names = []; //creates an empty array var values = [1,2,]; //AVOID! Creates an array with 2 or 3 items var options = [,,,,,]; //AVOID! creates an array with 5 or 6 items alert(colors.length); //3 alert(names.length); //0 alert(values.length); //2 (FF, Safari, Opera) or 3 (IE) alert(options.length); //5 (FF, Safari, Opera) or 6 (IE) var colors = ["red", "blue", "green"]; //creates an array with three strings colors.length = 2; alert(colors[2]); //undefined var colors = ["red", "blue", "green"]; //creates an array with three strings colors.length = 4; alert(colors[3]); //undefined var colors = ["red", "blue", "green"]; //creates an array with three strings colors[colors.length] = "black"; //add a color colors[colors.length] = "brown"; //add another color alert(colors.length); //5 alert(colors[3]); //black alert(colors[4]); //brown var colors = ["red", "blue", "green"]; //creates an array with three strings colors[99] = "black"; //add a color (position 99) alert(colors.length); //100 var colors = ["red", "blue", "green"]; //creates an array with three strings alert(colors.toString()); //red,blue,green alert(colors.valueOf()); //red,blue,green alert(colors); //red,blue,green var person1 = { toLocaleString : function () { return "Nikolaos"; }, toString : function() { return "Nicholas"; } }; var person2 = { toLocaleString : function () { return "Grigorios"; }, toString : function() { return "Greg"; } }; var people = [person1, person2]; alert(people); //Nicholas,Greg alert(people.toString()); //Nicholas,Greg alert(people.toLocaleString()); //Nikolaos,Grigorios
var colors = new Array(); //create an array var count = colors.push("red", "green"); //push two items alert(count); //2 count = colors.push("black"); //push another item on alert(count); //3 var item = colors.pop(); //get the last item alert(item); //"black" alert(colors.length); //2 var colors = ["red", "blue"]; colors.push("brown"); //add another item colors[3] = "black"; //add an item alert(colors.length); //4 var item = colors.pop(); //get the last item alert(item); //"black" var colors = new Array(); //create an array var count = colors.push("red", "green"); //push two items alert(count); //2 count = colors.push("black"); //push another item on alert(count); //3 var item = colors.shift(); //get the first item alert(item); //"red" alert(colors.length); //2 var colors = new Array(); //create an array var count = colors.unshift("red", "green"); //push two items alert(count); //2 count = colors.unshift("black"); //push another item on alert(count); //3 var item = colors.pop(); //get the first item alert(item); //"green" alert(colors.length); //2 var values = [1, 2, 3, 4, 5]; values.reverse(); alert(values); //5,4,3,2,1 var values = [0, 1, 5, 10, 15]; values.sort(); alert(values); //0,1,10,15,5 function compare(value1, value2) { if (value1 < value2) { return -1; } else if (value1 > value2) { return 1; } else { return 0; } } var values = [0, 1, 5, 10, 15]; values.sort(compare); alert(values); //0,1,5,10,15 function compare(value1, value2) { if (value1 < value2) { return 1; } else if (value1 > value2) { return -1; } else { return 0; } } var values = [0, 1, 5, 10, 15]; values.sort(compare); alert(values); //15,10,5,1,0
var colors = ["red", "green", "blue"]; alert(colors.join(",")); //red,green,blue alert(colors.join("||")); //red||green||blue var colors = ["red", "green", "blue", "yellow", "purple"]; var colors2 = colors.slice(1); var colors3 = colors.slice(1,4); alert(colors2); //green,blue,yellow,purple alert(colors3); //green,blue,yellow var colors = ["red", "green", "blue"]; var removed = colors.splice(0,1); //remove the first item alert(colors); //green,blue alert(removed); //red - one item array removed = colors.splice(1, 0, "yellow", "orange"); //insert two items at position 1 alert(colors); //green,yellow,orange,blue alert(removed); //empty array removed = colors.splice(1, 1, "red", "purple"); //insert two values, remove one alert(colors); //green,red,purple,orange,blue alert(removed); //yellow - one item array
var numbers = [1,2,3,4,5,4,3,2,1]; alert(numbers.indexOf(4)); //3 alert(numbers.lastIndexOf(4)); //5 alert(numbers.indexOf(4, 4)); //5 alert(numbers.lastIndexOf(4, 4)); //3 var person = { name: "Nicholas" }; var people = [{ name: "Nicholas" }]; var morePeople = [person]; alert(people.indexOf(person)); //-1 alert(morePeople.indexOf(person)); //0
var numbers = [1,2,3,4,5,4,3,2,1]; var filterResult = numbers.filter(function(item, index, array){ return (item > 2); }); alert(filterResult); //[3,4,5,4,3] var numbers = [1,2,3,4,5,4,3,2,1]; var mapResult = numbers.map(function(item, index, array){ return item * 2; }); alert(mapResult); //[2,4,6,8,10,8,6,4,2] var values = [1,2,3,4,5]; var sum = values.reduce(function(prev, cur, index, array){ return prev + cur; }); alert(sum);
//Date.now() is in ECMAScript 5 //Prior to that, use +new Date() var now = (typeof Date.now == "function" ? Date.now() : +new Date()); alert("Right now: " + now); var now = new Date(); alert(now); var someDate = new Date(Date.parse("May 25, 2004")); alert(someDate); //January 1, 2000 at midnight var y2k = new Date(Date.UTC(2000, 0)); alert(y2k.toUTCString()); //May 5, 2005 at 5:55:55 PM GMT var allFives = new Date(Date.UTC(2005, 4, 5, 17, 55, 55)); alert(allFives.toUTCString()); var date1 = new Date(2007, 0, 1); //January 1, 2007 var date2 = new Date(2007, 1, 1); //February 1, 2007 alert(date1 < date2); //true alert(date1 > date2); //false