I have noticed some difference in representing strings in DOM passed from server as HTML attribute or as global var.
There is a string which is JSON encoded object with special chars in it and set this string in folowing ways:
<div id="my-id"
data-opt='[{"id":"600900340","parent_id":"600900000","name":"\tCollector\u0027s Cars \u0026 Models","visible":"1","level":"3"}]'
></div>
<script>
window.opt = '[{"id":"600900340","parent_id":"600900000","name":"\tCollector\u0027s Cars \u0026 Models","visible":"1","level":"3"}]';
</script>
Now check them with js:
;(function(window) {
console.log(document.getElementById('my-id').dataset.opt);
console.log(window.opt);
}(window))
Output:
[{"id":"600900340","parent_id":"600900000","name":"\tCollector\u0027s Cars \u0026 Models","visible":"1","level":"3"}]
[{"id":"600900340","parent_id":"600900000","name":" Collector's Cars & Models","visible":"1","level":"3"}]
Why are they different?
Here is jsfiddle http://jsfiddle.net/9ss5M/3/
window.opt), and once in an HTML context where it is taken just literally (data-opt).