Also see Coffeescript.

  1. Block Scope in Javascript
  2. Grunt
  3. EmberJS
  4. Bower
  5. Chai and Mocha
  6. Leaflet
  7. Selectize and Select2

Some (very old) Javascript snippets:

Confirm Page Exit

To pop up a dialog saying “Are you sure you want to navigate away from this page? Press OK to continue, or Cancel to stay on the current page.” when you try to leave the page, you can use this:

<script type="text/javascript">
    function confirmExit() { return "You might lose your changes if you leave this page."; }
</script>

<body onbeforeunload="return confirmExit()"> ... </body>

This is an Internet Explorer-only feature (not in any public standard) but Firefox has implemented it as well.

Select All Checkboxes

To select all the checkbox elements in a <select> form (until it reaches an element with a name of ‘select_all’):

function selectAll(selectelem) {
	var form = selectelem.form;
	var i = 0;
	while (form[i].type == 'checkbox' && form[i].name != 'select_all') {
		form[i].checked = selectelem.checked;
		i++;
	}
}

Check E-mail Validity

A simple check to check the validity of e-mail addresses (I think it works for most cases):

function check_email(obj) {
	x = new RegExp("w+@w+.w");
	if (x.test(obj.value)) {
		return true;
	} else {
		alert("Your value of "" + obj.value + "" is not a valid e-mail address.");
		return false;
	}
}

You can either just go within a normal page to print out the entire page: window.print()

Or, you could be clever and create an IFRAME to print from:

var c = document.createElement("IFRAME");
c.innerHTML = "text you want to print";
// you may want to apply styles here as well
c.contentWindow.focus();
c.contentWindow.print();
document.body.removeChild(c);

Note that this can reduce accessibility.

Inline Associative Array Initialisation

var arr = {"key" : "value", "key2" : "value2"};