JavaScript
HTML
CSS
jQuery
PHP
SQL
WordPress
JavaScript Tutorials

Overview

JavaScript is one of the three main languages (along with HTML and CSS) for coding a website.

While HTML is the page structure, and CSS the page design, JavaScript can be considered the page “action”, as it is often used to interact with the user.

Example of Using JavaScript

				
					<!-- html element to place output -->
<p id="my_output"></p>
				
			
				
					// place text in the #my_output element
document.getElementById('my_output').innerHTML = 'Hello World';
				
			

Linking to an External Document

An external JavaScript file (or multiple files) are used to define the various site actions, while keeping everything centralized. This is a best practice because if you were to place your scripts directly in the head of each document (or scattered throughout the body), there would be no centralized location to make your changes and you’d have to search for all the various rules across multiple locations.

The following shows the head of our document, and how the link should be placed.

				
					<head>

<title>Advanced HTML Template</title>


<!-- styles -->

<!-- scripts -->
<script src="/scripts/main.js"></script>

</head>
				
			

Non-crucial scripts can be placed just before the closing /body tag which can speed up the page load time because the page can first render before applying the scripts.

				
					<!-- scripts -->
<script type="text/javascript" src="/scripts/main.js"></script>

</body>

</html>
				
			

Placing Scripts Directly on the Page

Scripts can be placed directly in the head of the document or even inline with the element they are modifying, but neither are good practice.

However, if you must place them in the document itself, the following shows where we want to place our script(s). Typically this is done directly after the title tag.

				
					<head>

<title>Advanced HTML Template</title>


<!-- styles -->


<!-- scripts -->
<script>
    function myFunction() {
        var element = document.getElementById('myDIV');
        element.classList.add('mystyle');
    }
</script>

</head>

				
			

Scripts can also be placed just before the closing /body tag which can speed up the page load time because the page can first render before applying the scripts.

				
					<!-- scripts -->
<script>
    function myFunction() {
        var element = document.getElementById('myDIV');
        element.classList.add('mystyle');
    }
</script>
<noscript>Sorry, your browser does not support JavaScript!</noscript>

</body>

</html>
				
			

Inline Scripting

Another way to place styles (again, not the best way to do it!), is to place them directly with the element they are interacting with.

The following has a call to the JavaScript right within the button element. And the script is just below it.

Again, this is not a great practice and makes it difficult to locate the scripting and make changes, especially if it occurs on multiple pages.

				
					<!-- html element to place output -->
<p id="my_output">My content</p>

<button type="button" onclick="myFunction()">Change Text</button>

<script>
function myFunction() {
    document.getElementById('my_output').innerHTML = 'New content';
}
</script>
				
			

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.

JavaScript
HTML
CSS
jQuery
PHP
SQL
WordPress

Why 1SMARTchicken?

This site was built and is maintained to benefit my autistic son.
See More →

My Son's Name is Johnny

He was diagnosed as autistic quite late, at age four...
His story

Buy Me a Coffee

Thanks for your support!

Feedback

If you see an error on the page or the code itself is incorrect or incomplete, or just plain wrong, please let us know. We’re always learning. NOTE: we do not sell your information and will not send you spam emails.