First thing first. This may be basic but , nice to know to beginner. You need to run your code after components loaded. So trigger your javascript code writing in $( document ).ready(function(){} block.
<html>
<head>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$( document ).ready(function() {
console.log( "document loaded" );
});
$( window ).on( "load", function() {
console.log( "window loaded" );
});
</script>
</head>
<body>
...
</body>
</html>