Automatically translated.View original post

javascript writing format in conjunction with html

# Web Site Writing Basics

# javascript

Writing JavaScript (JS) in conjunction with HTML can take many forms, depending on the needs and complexity of the project. Here are some of the main formats used:

1. Embedding code in the < script > tag (Internal JS)

You can write JavaScript code directly within the same HTML file. Using the < script > tag, it is typically placed at the end of the < body > tag, ensuring that all HTML elements are finished loading before the script starts.

Example:

HTML

< html >

< head >

< title > Internal JavaScript < / title >

< / head >

< body >

< h1 > Internal JavaScript implementation < / h1 >

< button onclick = "showAlert ()" > Click here < / button >

< script >

/ / The JavaScript code is here.

showAlert function () {

alert ("Hello! This is a message from Internal JavaScript");

}

< / script >

< / body >

< / html >

2. External JS file link

This is the most recommended format for large projects or projects with a lot of JavaScript code. You will write the entire JavaScript code in a separate file (usually ending with the extension .js) and link it to an HTML file using the < script > tag with the src attribute.

Advantages:

Easy to handle: HTML and JavaScript code are separate, making it easier to maintain and read.

Reusable: A single JS file can be combined with multiple HTML pages.

Performance: The browser can cache (Cache) external JS files, making them load faster on the next visit.

Example:

index.html file:

HTML

< html >

< head >

< title > External JavaScript < / title >

< / head >

< body >

< h1 > External JavaScript implementation < / h1 >

< button id = "myButton" > Click here < / button >

< script src = "script.js" > < / script >

< / body >

< / html >

File script.js:

JavaScript

/ / External JavaScript code is in this file.

document.getElementById ('myButton') .addEventListener ('click', function () {

alert ("Hello! This is a message from External JavaScript");

});

3.Writing code in the HTML attribute (Inline JS)

This is to write short JavaScript code directly in the attributes of HTML tags such as onclick, onmouseover, or onchange.

Caution:

Not recommended for complex code or bulk code

This makes the code difficult to read and difficult to maintain.

Against the Separation of Concerns

Example:

HTML

< html >

< body >

< h1 > Inline JavaScript implementation < / h1 >

< button onclick = "alert ('Hello from Inline JS!');" > Click here < / button >

< / body >

< / html >

Summary and Recommended Practices

Format Method Recommended for

External JS External .js file. Welded with < script src = "file .js" > < / script > all projects, especially large projects ().

Internal JS code in a < script > tag inside a short HTML code file or a simple experiment.

Inline JS code in HTML attributes (e.g. onclick) Avoid if you can, used only for simplest functions

2025/9/28 Edited to

... Read moreนอกจาก 3 รูปแบบหลัก (Inline / Internal / External) ที่หลายคนเจอเวลาเริ่มเขียน html javascript แล้ว อีกเรื่องที่ทำให้ “โค้ดไม่ทำงาน” บ่อยมากคือเรื่องตำแหน่งการวาง <script> และการรอให้ DOM โหลดเสร็จก่อนค่ะ 1) วาง <script> ไว้ตรงไหนดีถึงปลอดภัย? - ถ้าวางไฟล์ External JS ไว้ท้าย </body> (ก่อนปิดแท็ก) ส่วนใหญ่จะเวิร์กสุด เพราะ HTML โหลดปุ่ม/องค์ประกอบต่างๆ เสร็จก่อน แล้วค่อยรัน JS - ถ้าจำเป็นต้องวางไว้ใน <head> แนะนำใส่ attribute defer เช่น <script src="script.js" defer></script> ข้อดีคือไฟล์ JS จะโหลดไปพร้อมๆ กับ HTML แต่จะรันหลังจาก DOM สร้างเสร็จ ทำให้ document.getElementById(...) หา element เจอ 2) ต่างกันยังไงระหว่าง defer กับ async? - defer: โหลดระหว่าง parse HTML และ “รันหลัง DOM พร้อม” (เหมาะกับสคริปต์ที่ต้องจับปุ่ม/ผูก event) - async: โหลดและรันทันทีที่โหลดเสร็จ อาจรันก่อน DOM พร้อม (เหมาะกับสคริปต์ที่ไม่พึ่ง element เช่น analytics บางแบบ) 3) แนะนำเลิกใช้ Inline JS แล้วผูก event ในไฟล์แทน Inline แบบ onclick="..." ใช้ง่ายก็จริง แต่พอโค้ดยาวขึ้นจะดูแลยากมาก ฉันมักเปลี่ยนเป็นใส่ id หรือ class แล้วไป addEventListener ใน JS เช่น HTML: <button id="myButton">คลิกที่นี่</button> JS: document.getElementById('myButton').addEventListener('click', showAlert); function showAlert(){ alert('สวัสดี!'); } แบบนี้แยกหน้าที่ชัด (HTML จัดโครง, JS จัดพฤติกรรม) 4) ถ้าใช้ Internal JS แนะนำเขียนให้เป็นระเบียบ ถ้าอยากลองอะไรเร็วๆ ในไฟล์เดียว ให้ใส่ <script> ไว้ท้าย body และตั้งชื่อฟังก์ชัน/คอมเมนต์ให้ชัด จะอ่านง่ายกว่าเขียนปนกับ onclick เยอะๆ 5) เช็กลิสต์เวลา JS ไม่ทำงาน (เจอบ่อยมาก) - สะกด id ให้ตรง (myButton vs mybutton) - ลืมใส่ # ตอน querySelector เช่น document.querySelector('#myButton') - วาง <script> ไว้ใน head แต่ไม่ใช้ defer - เปิด Console แล้วมี error แต่ไม่ได้ดู (กด F12 > Console) สรุปส่วนตัว: ถ้าเป็นงานจริงหรือทำหลายหน้า ฉันเลือก External JS เป็นหลัก แล้วใช้ defer หรือวางท้าย </body> เพื่อให้การใช้งาน html javascript ลื่นและดูแลง่ายกว่าในระยะยาวค่ะ