Tutorials

KodeKite Tools

HTML
Flexbox
SVG
Canvas
HTML Tutorial

More


HTML JS Use
JavaScript Use

JavaScript is used in HTML to change behaviour of elements like it can change the content of html elements, it can call functions to perform different actions etc. JavaScript is inserted in two ways within HTML page.


  • External js
  • Internal js
External JS

External js is inserted in HTML page by using <script> element. The benifit of using external js is it separate HTML code form js code and makes code easy to understand and you can apply same js file to multiple pages.

Syntax

<script src=js file></script>


You can insert any number of js files using script element.


Note:

You can give any name to js file but do not forget to give .js extension after js file name


Example

Paragraph

document.getElementById (p) .innerHTML=The external js file has following code;
Internal JS

Internal js is inserted in HTML page by using <script> element, if your web page has different functions or you have wanrt to apply funtions to single page then internal js is good.

Syntax

<script>
js code
</script>


You can use any number of js files using script element.


Note:

You can give any name to js file but don’t forget to give .js extension after js file name


Example

Internal js File


Don't forget that you can use multiple js files in a single HTML page.


Practice Task

Open notepad or any other text editor, follow the following points.


  • Create any HTML element
  • Use internal js
  • Assign element an id with name myid
  • Now use js to get element id and change color of HTML element
  • Color should be purple

save file with name task.html or task.htm and then open it using any browser and see results.


Solution

Paragraph with internal js


You can use javascript any where within HTML page.