Frits was able to figure out your problem, which was good. I would definitely take his advice and move away quickly from using body onload
.
For instance:
(function run(){
var data = '{"id":1,"name":"Joe Smith","data":{"email":"[email protected]","phone":"555-123-4567","title":"CFO","instanceControllerTagLibraryApi":{"developmentMode":false}},"children":[],"instanceControllerTagLibraryApi":{"developmentMode":false}}';
function init(){
console.log(data, JSON.parse(data));
}
if (window.addEventListener) {
window.addEventListener('load', init);
} else if (window.attachEvent) {
window.attachEvent('load', init);
}
})();
http://jsfiddle.net/userdude/eQRBk/1/
Notice the use of window.addEventListener
(and the legacy window.attachEvent
for older IE versions). And that console.log
works better in Chrome and Firebug; IE's console is a little more frustrating in how it logs objects, in my opinion.
Using what you've given, you could (and I would suggest should) handle it differently. Forgive me, however, I'm not that well-versed in Java and JSP is all I know (barely). So...?
<%
String jsonString = "'{\"id\":1,\"name\":\"Joe Smith\",\"data\":{\"email\":\"[email protected]\",\"phone\":\"555-123-4567\",\"title\":\"CFO\",\"instanceControllerTagLibraryApi\":{\"developmentMode\":false}},\"children\":[],\"instanceControllerTagLibraryApi\":{\"developmentMode\":false}}'";
%>
<script>
(function run(){
var data = <%=jsonString%>;
function init(){
console.log(data, JSON.parse(data));
}
if (window.addEventListener) {
window.addEventListener('load', init);
} else if (window.attachEvent) {
window.attachEvent('load', init);
}
})();
</script>
And then you can insert that JSP page into the head
or the body
and it will work fine. I also imagine you could set it up to dynamically inject that data into the JSP page. But, like I said, I'm kinda waving my hands here.
\"
with simple quotes'
?