แท็ก <embed> ใน HTML คืออะไร?
แท็ก <embed> ใช้สำห ฝังแอปพลิเคชันหรือเนื้อหาภายนอก (External Content) ลงในหน้า HTML** เช่น Flash (SWF), PDF, Video, Audio, SVG, 3D Models เป็นต้น เป็น Void Element (ไม่มีแท็กปิด)
แอตทริบิวต์หลัก
| แอตทริบิวต์ | คำอธิบาย |
|---|---|
| src | URL ของไฟล์ที่จะฝัง (Required) |
| type | MIME Type ของไฟล์ (เช่น application/pdf, video/mp4) |
| width | ความกว้างในพิกเซล |
| height | ความสูงในพิกเซล |
ตัวอย่างการใช้งาน
<!DOCTYPE html>
<html>
<body>
<h2>แอปพลิเคชันสาธิต CSS</h2>
<embed src="https://www.example.com/flex/samples/CSSApplication.swf"
width="600" height="400"
type="application/x-shockwave-flash">
</body>
</html>
ผลลัพธ์
แสดงแอปพลิเคชัน Flash (SWF) ขนาด 600x400 พิกเซล
<embed> vs <object> vs <iframe>
| แท็ก | วัตถุประสงค์ | Fallback | HTML5 |
|---|---|---|---|
<embed> | ฝัง Plugin/External Content (Simple) | ไม่มี | รองรับ |
<object> | ฝัง Object พร้อม Fallback & Params | มี (Child Content) | รองรับ |
<iframe> | ฝังหน้าเว็บอีกหน้า (Browsing Context) | ไม่มี | รองรับ |
<video>/<audio> | ฝัง Media Native (HTML5) | มี (Source + Text) | แนะนำสำหรับ Media |
<img> | ฝังรูปภาพ | alt text | แนะนำสำหรับ Images |
MIME Types พบบ่อยสำหรับ embed
application/x-shockwave-flash- Flash (SWF) **Deprecated**application/pdf- PDFvideo/mp4,video/webm- Videoaudio/mpeg,audio/ogg- Audioimage/svg+xml- SVGmodel/gltf-binary- 3D Model (glTF)
จุดสำคัญ
<embed>ไม่มี Fallback Content ใช้<object>ถ้าต้องการ Fallback- Flash Player จบชีวิต 2020 ไม่ควรใช้ SWF ใหม่
- สำหรับ Video/Audio ใช้
<video>,<audio>Native HTML5 typeattribute ช่วยเบราวเซอร์เลือก Plugin/Handler ที่เหมาะสม- Security: ใส่
sandboxบน iframe แทน embed สำหรับ Untrusted Content