Skip to content

Instantly share code, notes, and snippets.

@monsur
Created July 30, 2012 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monsur/3211294 to your computer and use it in GitHub Desktop.
Save monsur/3211294 to your computer and use it in GitHub Desktop.
Generate a table comparing the differences between the escape, encodeURI, and encodeURIComponent functions
function createRow(c, tr) {
var td = document.createElement('td');
td.align = 'center';
td.innerHTML = c;
if (c[0] !== '%') {
td.style.backgroundColor = '#ffc';
}
tr.appendChild(td);
}
function createTable(tableId) {
for (var i = 0; i < 128; i++) {
var c = String.fromCharCode(i);
var c1 = escape(c);
var c2 = encodeURI(c);
var c3 = encodeURIComponent(c);
if ((c1 === c2) && (c2 === c3)) {
continue;
}
var tr = document.createElement('tr');
var td = document.createElement('td');
td.align = 'center';
td.innerHTML = c;
tr.appendChild(td);
createRow(c1, tr);
createRow(c2, tr);
createRow(c3, tr);
document.getElementById(tableId).appendChild(tr);
}
}
createTable('results');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment