Sposti tutto nella head e scateni l'operazione alla onload tipo:
onload= function(){
var elemento = document.getElementById('id_elemento');
.....}