<HTML>
<body>
<script>
test = document.querySelector('#text');
alert(test);
</script>
<p id="text">testing</p>
</body>
</html>
<HTML>
<body>
<script>
function onload() {
test = document.querySelector('#text');
alert(test);
}
document.addEventListener("DOMContentLoaded", onload);
</script>
<p id="text">testing</p>
</body>
</html>