I have decided to spend more of my time and energy learning more JavaScript and to keep the notes here for me to look back at, as well as to help others who may also be interested in learning more JavaScript programming.
I don’t think JavaScript is going away anytime soon because it’s now on most every website out there. Not only is it on the client side of things, but with node.js, we can even build the backend with JavaScript.
I think it will be time well spent to learn it.
To add JavaScript to an HTML page:
<!-- inline code --> <script> //javascript code </script> <!-- external javascript file --> <script src="https://cullenwebservices.com/js/somejavascript.js"></script>
The type
attribute is not necessary in the script tag because it is the default and no other options are really noticed by today’s current browsers.
Inline JavaScript code is interpreted and evaluated before the rest of the page content is loaded, unless it is a function or object and then it’s executed when it’s called. This is a little confusing, but definitely important. It usually makes sense to put the JavaScript at the bottom of the page so that it doesn’t slow down the page loading and so that all the elements that might be used by the JavaScript actually exist before they are used.
If you have </script>
anywhere in your javascript code, it must be escaped to work correctly in an HTML page like this: <\/script>
The external JavaScript file can be outside the domain, so scripts from other sites can be used in your web page.
<script>
tags and code are evaluated in the order they appear in the HTML page.
async
and defer
are attributes worth mentioning but I probably won’t use them because they aren’t implemented in all browsers. They determine when the JavaScript should be loaded, but if all browsers don’t implement them, they aren’t worth using… yet. Actually, even if they are being implemented, putting the scripts at the bottom of the page should solve both problems without using these attributes.