Not enough reputation to comment yet so I'll just put this here:
To expand on Micah's answer - the browser runs your code top to bottom, so if you write:
<div id="chart"></div>
<script>var svg = d3.select("#chart").append("svg:svg");</script>
The browser will create a div with id "chart", and then run your script, which will try to find that div, and, hurray, success.
Otherwise if you write:
<script>var svg = d3.select("#chart").append("svg:svg");</script>
<div id="chart"></div>
The browser runs your script, and tries to find a div with id chart, but it hasn't been created yet so it fails.
THEN the browser creates a div with id "chart".