My webpage has the following code:
This is test Page
Your script uses dom element and must run after the dom loaded.
Wrap your code in a function and call it after dom loaded
function myfunc(){ //code here } window.onload = myfunc();