3 Ways to Add JS to HTML

1. Internal

The 1st one is by placing the <script> tag in the <head> or in the <body>.

if you placed in the body make sure it’s before the closing <body> tag, so your scripts will run for all the elements in the bod…


This content originally appeared on DEV Community and was authored by Med Amine Fh

1. Internal

The 1st one is by placing the <script> tag in the <head> or in the <body>.

if you placed in the body make sure it's before the closing <body> tag, so your scripts will run for all the elements in the body.

Internal JS

This way is not for daily use or for big projects, because the html file will be complicated more and more.

2. External

The 2nd is by creating a JS file and link it inside your HTML .

In this example we created a Javascript file named script.js and linked it using the script tag. put the path to your JS file in the src.

External JS

3. Inline

The last way is the inline way. by adding an event listener inside any HTML element you want to run your scripts on.

Inline Js

SO the above code runs when the user clicks on the body

The common used way for daily programming is the External way


This content originally appeared on DEV Community and was authored by Med Amine Fh


Print Share Comment Cite Upload Translate Updates
APA

Med Amine Fh | Sciencx (2021-10-17T14:22:03+00:00) 3 Ways to Add JS to HTML. Retrieved from https://www.scien.cx/2021/10/17/3-ways-to-add-js-to-html/

MLA
" » 3 Ways to Add JS to HTML." Med Amine Fh | Sciencx - Sunday October 17, 2021, https://www.scien.cx/2021/10/17/3-ways-to-add-js-to-html/
HARVARD
Med Amine Fh | Sciencx Sunday October 17, 2021 » 3 Ways to Add JS to HTML., viewed ,<https://www.scien.cx/2021/10/17/3-ways-to-add-js-to-html/>
VANCOUVER
Med Amine Fh | Sciencx - » 3 Ways to Add JS to HTML. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/17/3-ways-to-add-js-to-html/
CHICAGO
" » 3 Ways to Add JS to HTML." Med Amine Fh | Sciencx - Accessed . https://www.scien.cx/2021/10/17/3-ways-to-add-js-to-html/
IEEE
" » 3 Ways to Add JS to HTML." Med Amine Fh | Sciencx [Online]. Available: https://www.scien.cx/2021/10/17/3-ways-to-add-js-to-html/. [Accessed: ]
rf:citation
» 3 Ways to Add JS to HTML | Med Amine Fh | Sciencx | https://www.scien.cx/2021/10/17/3-ways-to-add-js-to-html/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.