Blog for Learning

| lesson material | material summary | questions and answers | definitions | types and examples | other information | materi pelajaran | ringkasan materi | pertanyaan dan jawaban | definisi | jenis-jenis dan contoh-contoh | informasi lainnya |

Powered by Blogger.

Pages

4 Ways to Write Javascript Code in HTML

Javascript Programming
Javascript as a programming language that runs on top of the browser must be written in HTML. There are four ways to write javascript in HTML.

1. Tag <script>

The common method used is to write it in tags <script>Tags <script>can be made in tags <head>, whatever in tags <body>Example:
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Penulisan Javascript</title>
    <script>
    console.log("Hi, ini kode Javascript");
    </script>
</head>
<body>

<script>
document.write("Javascript itu keren!");
</script>
</body>
</html>

2. External Files

Do not want the Javascript code to mix with HTML, we can write it in a separate file. The trick, make a file that has an extension .js, for example the contents are as follows.
// file-eksternal.js
alert("Kode Javascript dari File Eksternal");
Then, we need to connect the external file with the HTML file. The method is to use tags <script>with attributes srcto determine the location of the Javascript file.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Penulisan Javascript</title>

<script src="file-eksternal.js"></script>


</head>
<body>

</body>
</html>

3. Event Attributes

The third method is often used to call functions at certain events . For example when an element is clicked, then run the Javascript function.
<button onclick="alert('Ok Terima kasih!')">Klik donk!</button>
Besides onclick events there are also other events , such as onsubmit , onload , ondoubleclick , onmouseover , onmouseout, etc.

4. URL

Finally, write Javascript in the URL. This method is rarely used, but we also need to know it. Writing Javascript on a URL using the Javascript protocol. For example, try to write code like this in the browser URL.
javascript:alert("Nah! ini Javascript")
The result, Javascript will be executed by the browser.
Javascript call on URL
Then, how do we use this method in HTML?
We can use this method on tags <a>, then fill in the javascript code in the attribute hrefThis method can replace the onclick event .
<a href="javascript:alert('Wih! hebat bukan?')">Klik Aku</a>
That's the four ways to write Javascript in HTML. Which way do you use often?
0 Komentar untuk "4 Ways to Write Javascript Code in HTML"

Silahkan berkomentar sesuai artikel

 
Template By Kunci Dunia
Back To Top