Skip to content

Instantly share code, notes, and snippets.

@chilversc
Created March 30, 2012 17:12
Show Gist options
  • Select an option

  • Save chilversc/2253032 to your computer and use it in GitHub Desktop.

Select an option

Save chilversc/2253032 to your computer and use it in GitHub Desktop.
IE 8 JSON madness
<html>
<head>
<title></title>
<meta http-equiv="X-UA-Compatible" content="IE=8">
</head>
<body>
<h1>Test</h1>
<pre id="testout"></pre>
<input type="text" id="testin" value="">
<h1>My output</h1>
<pre>
x =
x.toString() =
x.toJSON() =
"".toJSON() =
x === null = false
x === "" = true
typeof x = string
JSON.stringify(x) = "null"
JSON.stringify(""+x) = "null"
JSON.stringify(x+"") = ""
JSON.stringify(null) = null
JSON.stringify("") = ""
</pre>
<script>
document.getElementById("testin").value = "";
var x = document.getElementById("testin").value;
var y = "";
y += 'x = ' + x + "\n";
y += 'x.toString() = ' + x.toString() + "\n";
if (String.prototype.toJSON) {
y += 'x.toJSON() = ' + x.toJSON() + "\n";
y += '"".toJSON() = ' + ("".toJSON()) + "\n";
}
y += 'x === null = ' + (x === null) + "\n";
y += 'x === "" = ' + (x === "") + "\n";
y += 'typeof x = ' + (typeof x) + "\n";
y += 'JSON.stringify(x) = ' + JSON.stringify(x) + "\n";
y += 'JSON.stringify(""+x) = ' + JSON.stringify(""+x) + "\n";
y += 'JSON.stringify(x+"") = ' + JSON.stringify(x+"") + "\n";
y += 'JSON.stringify(null) = ' + JSON.stringify(null) + "\n";
y += 'JSON.stringify("") = ' + JSON.stringify("") + "\n";
document.getElementById("testout").innerText = y;
</script>
</body>
</html>
@chilversc
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment