{"id":9101,"date":"2020-09-05T07:06:36","date_gmt":"2020-09-05T11:06:36","guid":{"rendered":"http:\/\/local.brightwhiz\/?p=9101"},"modified":"2021-12-04T07:11:40","modified_gmt":"2021-12-04T07:11:40","slug":"where-to-add-javascript-html-file","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/where-to-add-javascript-html-file\/","title":{"rendered":"This is How to Add JavaScript to an HTML File"},"content":{"rendered":"\n

This is a simple guide that shows you how to add JavaScript to an HTML file in order to implement its interactive features on a webpage.<\/p>\n\n\n\n

JavaScript is a scripting language that can be implemented by using JavaScript statements that are usually embedded or linked to a webpage.<\/p>\n\n\n\n

There are two generally accepted ways to add JavaScript to an HTML file and both require a special HTML SCRIPT tag.<\/p>\n\n\n\n

Add JavaScript With the HTML SCRIPT Tag<\/h2>\n\n\n\n

SCRIPT tags that come in the form of:<\/p>\n\n\n\n

<script>\n   \u2026\n<\/script><\/code><\/pre>\n\n\n\n

Can be placed literally anywhere within your HTML document but it is recommended that you should keep it within the HEAD tags. For performance reasons, the other recommended place to insert JavaScript files is at the end of the HTML file just before the closing BODY tag<\/p>\n\n\n\n

You need to do this because the SCRIPT tags alert the browser program to start interpreting all the text between these tags as a script<\/a>.<\/p>\n\n\n\n

Therefore you need to place your JavaScript code within the tags as follows:<\/p>\n\n\n\n

<script ...="">\n   JavaScript code\n<\/script><\/code><\/pre>\n\n\n\n

Here is a simple example that allows us to print out “Hello World<\/strong>“. We do this by calling the JavaScript function document.write<\/em>, which takes a string parameter of what we want to print out.<\/p>\n\n\n\n

With this function, you can write out plain text or even HTML. This is the basic syntax.<\/p>\n\n\n\n

<script>\n    document.write("Hello World!")\n<\/script><\/code><\/pre>\n\n\n\n

In the real world, you will find the need to implement JavaScript code that spans several hundred lines of code, if not thousands. Furthermore, you may find yourself needing to implement your scripts code from multiple sources.<\/p>\n\n\n\n

Embedding this code in SCRIPT tags might not be the most ideal situation. The solution here would be to link to an external JavaScript file that usually ends with a .js<\/em> file extension.<\/p>\n\n\n\n

To do this you would simply modify the SCRIPT tag and add the SRC attribute which holds the URL or relative path to the JavaScript file.<\/p>\n\n\n\n

Here are three main reasons why you would want to link to external JavaScript files.<\/p>\n\n\n\n