Various writing ways of JavaScript
Goal
- โก Learn different writing styles of javascript
Overview
In this step, we'll be learning the different writing styles of javascript which is doing the same things.
This is similar with what we did in CSS section.
Ref: https://www.w3schools.com/jsref/event_onclick.asp
element.onclick = function(){do something};
1. This is what we did in the last step.
<element onclick="do something">
2. Check it also works.
element.addEventListener
3. 4. Separate js file
You can separate js code to another file, then import that file.
Make js-test1.js
Import js-test1.js
in js-test1.html
You see it also works.
head
tag
Import in You can import script file inside <head>
tag.
https://www.w3schools.com/js/js_whereto.asp
caution
But, if you import script tag in <head>
, it doesn't work.
This is because
- HTML file is read from top to bottom by browser.
- When js file is read
is not loaded yet.
That's why the below code cannot find DOM element.
So, it doesn't work correctly.
Summery
info
It is enough to know there are various writing styles which is doing the same thing in javascript. ๐