I’ve received much excellent instruction on Stack Overflow, especially regarding my feeble attempts to incorporate asynchronous processing in a recent web application. To narrow down some issues to their minimum, I created a very small HTML/javascript page to play with getJSON and look at some behavior mentioned by jfriend00. So far as I can see, this is a legitimate program, but although IE9 runs it, FireFox emits some text, then hangs/infinitely loops/whatever, while Chrome shows only the H1 (FireFox declined to do so) and the last string. Obviously, there is something horribly wrong with this code and I’m not seeing it. How about you?
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
</head>
<body>
<h1>testing 2</h1>
<script type="text/javascript">
function buildTree() {
$.getJSON('MurakiMaida.json', function(data) {
document.write("how about here?<br>");
$.each(data.person, function(i, xdata) {
document.write(xdata.id + "<br>");
});
});
document.write("<br>what are we doing here?");
}
buildTree();
</script>
</body>
</html>
document.writeis probably the culprit.I don’t understand what exactly you are try to do, but
document.writeshould only be used while the page is loading. Actually it is probably better to never use it.*Create a
<div id='foo'/>and write to it such as$('#foo').append($("<div>"+xdata.id+"</div>")document.writewas the way to use Javascript to add HTML to a document back in the days before the DOM existed. It still exists for backward compatibility, but should be avoided.