{"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
SCRIPT tags that come in the form of:<\/p>\n\n\n\n
<script>\n \u2026\n<\/script><\/code><\/pre>\n\n\n\nCan 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\nTherefore 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\nHere 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\nWith 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\nIn 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\nTo 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
- Portability<\/strong>: Externally linked scripts allow you to use that code on multiple web pages. It also allows you to maintain the source code independently of your HTML<\/li>
- Codesharing<\/strong>: This allows you to share your JavaScript code as libraries that others can use. You also have the option of using other developers’ libraries<\/li>
- Performance<\/strong>: externally linked scripts especially those loaded from another domain or server like a CDN (content delivery network) help reduce the number of HTTP<\/a> requests. It also allows these filed to be cached locally by the web browser<\/li><\/ul>\n\n\n\n
Following is some very basic example code to add JavaScript in an HTML file That prints out:<\/p>\n\n\n\n
Hello World From Script!\nHello World From Embedded!<\/code><\/pre>\n\n\n\nThis is the contents of the HTML file:<\/p>\n\n\n\n
<meta charset="UTF-8"> \n<title>Document<\/title>\n<script src="script.js" defer=""><\/script> \n \n<h1>Embed javascript in html file<\/h1>\n<div id="elem"><\/div>\n<script>\n let eleme = document.getElementById("elem");\n eleme.innerHTML += "Hello World From Embedded!<br>";\n<\/script><\/code><\/pre>\n\n\n\n